Java Program to find Factorial of a Number using Recursion - The Coding Shala

Home >> Java Programs >> Factorial Program using Recursion

 In this post, we will learn how to find Factorial of a Number using Recursion in Java.

Java Program to find Factorial of a Number using Recursion

Factorial of a non-negative number is the multiplication of numbers from 1 to n [n including]. Factorial of n is represented by n!.

n! = 1 * 2 * 3 * 4........ * (n-1) * n

Example 1:
Input: 5
Output: 120
Explanation: 1 * 2 * 3 * 4 * 5 = 120

Java Program: 

// Java program to Find Factorial using Recursion

import java.util.Scanner;

public class Main {
	
	public static long findFact(long num) {
		// break condition
		if(num <= 0) {
			return 1;
		} 
		return num * findFact(num-1);
	}
	
	
	public static void main(String[] args) { 
		Scanner sc = new Scanner(System.in);
		
		// take number input
		System.out.println("Enter the number");
		long num = sc.nextLong();
		
		long fact = findFact(num);
		
		System.out.println("Factorial of " + num + " is: " + fact);
		
		sc.close();
	}
}

Output: 

Enter the number
10
Factorial of 10 is: 3628800


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

LeetCode - Swap Nodes in Pairs Solution - The Coding Shala