Java Program to Find Factorial of a Number by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Add All the Numbers between a to b by Using Recursion

In this program we are going to see how to find factorial of a number using Recursion by Java programming language.

Java Program to Find Factorial of a Number by Using Recursion

Now let’s see different ways to find factorial of a number by Using Recursion.

Method-1: Java Program to Find Factorial of a Number  By Using Static Input and Recursion

Approach:

  • Declare and initiate two static integer variable say count and fact with the value of 0 and 1 respectively.
  • Declare and initiate an integer value n with a value.
  • Then call a user defined method calculateValue() and pass n as parameter.
  • Inside the user defined method write the logic to find factorial by using an If statement.
  • Call the same method inside that user defined method recursively till the target.
  • Print the result.

Program:

class Main
{
    //Declare and initiate two static integer variable say count and fact with the value of 0 and 1 respectively. 
    static int count=0,fact=1;
    public static void main(String[] args)
    {
        int n=10;
        calculateValue(n);
        System.out.print("Factorial of "+n+" is= "+fact);
    }
    
    //define the method
    public static void calculateValue(int n)
    {
        //Increase the value of count by 1
        count++;
        //Check the condition whether the value of count is continuing till the value reaches.
        if(count<=n)
        {
            fact*=count;
            //call the same function inside this user defined method
            calculateValue(n);
        }
    }
}
Output:

Factorial of 10 is= 3628800

Method-2: Java Program to Find Factorial of a Number  By Using User Input and Recursion

Approach:

  • Declare and initiate two static integer variable say count and fact with the value of 0 and 1 respectively.
  • Declare and initiate an integer value n and prompt the user to enter the value.
  • Then call a user defined method calculateValue() and pass n as parameter.
  • Inside the user defined method write the logic to find factorial by using an If statement.
  • Call the same method inside that user defined method recursively till the target.
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    //Declare and initiate two static integer variable say count and fact with the value of 0 and 1 respectivily. 
    static int count=0,fact=1;
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n=sc.nextInt();
        calculateValue(n);
        System.out.print("Factorial of "+n+" is= "+fact);
    }
    
    //define the method
    public static void calculateValue(int n)
    {
        //Increase the value of count by 1
        count++;
        //Check the condition whether the value of count is continuing till the value reaches.
        if(count<=n)
        { 
            fact*=count;
            //call the same function inside this user defined method
            calculateValue(n);
        }
    }
}
Output:

Enter a number: 5
Factorial of 5 is= 120

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: