LAST UPDATED: APRIL 26, 2022
Java Program to Find the Factorial of a Number using Recursion
In this tutorial, we will learn how to find the factorial of a number using a recursive function. A recursive function is a function that calls itself. But before moving forward if you are not familiar with the basic concepts of methods in java, then do check the article on the topic methods in java.
Input: Enter the number: 5
Output: The factorial of the entered number is: 120
Method 1: Java Program to Find the Factorial of a Number using Recursion
In this program, we will find the factorial of a number using recursion with user-defined values. Here, we will ask the user to enter a value and then we will calculate the factorial by calling the function recursively.
Algorithm
- Start
- Declare a variable to store a number.
- Ask the user to initialize the number.
- Check whether it is possible to calculate the factorial or not.
- If the number is greater than and equal to 0, then call a recursive function to calculate the factorial of the entered number.
- If the number is lesser than 0, print the message that it is not possible to calculate the factorial.
- If the entered number is 0 or 1, then return 1.
- If the entered number is other than 0 or 1, then calculate the factorial by recursively calling the same method.
- Return the result.
- Print the factorial of the entered number.
- Stop
Below is the code for the same in Java language.
/*Java Program to find factorial of a number using Recursive Function*/
import java.util.Scanner;
public class Main
{
//Driver Code
public static void main(String[] args)
{
//Take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number :");
int num = sc.nextInt(); //Input the number
if(num>=0)
{
//Call a recursive function to find the factorial
int factorial=findFactorial(num);
System.out.println("The factorial of the entered the number is :"+factorial);
}
else
{
System.out.println("Factorial not possible.");
System.out.println("Please enter valid input.");
}
}
//Recursive Function to Find the Factorial of a Number
public static int findFactorial(int num)
{
if(num==0)
return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
}
}
Enter the number: 10
The factorial of the entered number is:3628800
Method 2: Java Program to Find the Factorial of a Number using Recursion
In this program, we will find the factorial of a number using recursion with pre-defined values. Here, the number whose factorial is to be calculated is already given in the program and our task is to calculate the factorial by calling the function recursively.
Algorithm
- Start
- Declare a variable to store a number.
- Initialize the number.
- Check whether it is possible to calculate the factorial or not.
- If the number is greater than and equal to 0, then call a recursive function to calculate the factorial of the entered number.
- If the number is lesser than 0, print the message that it is not possible to calculate the factorial.
- If the entered number is 0 or 1, then return 1.
- If the entered number is other than 0 or 1, then calculate the factorial by recursively calling the same method.
- Return the result.
- Print the factorial of the entered number.
- Stop
Below is the code for the same in Java language.
/*Java Program to find factorial of a number using Recursive Function*/
public class Main
{
//Driver Code
public static void main(String[] args)
{
int num=5;
System.out.println("The entered number is :"+num);
if(num>=0)
{
//Call a recursive function to find the factorial
int factorial=findFactorial(num);
System.out.println("The factorial of the entered number is :"+factorial);
}
else
{
System.out.println("Factorial not possible.");
System.out.println("Please enter valid input.");
}
}
//Recursive Function to Find the Factorial of a Number
public static int findFactorial(int num)
{
if(num==0)
return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
}
}
The entered number is:5
The factorial of the entered number is:120