Java Program to Find Factorial of a Number Using Recursion

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to find factorial of a number using recursion. Factorial of a number is the product of itself with each of its lower numbers. Factorial is a function applied to natural numbers greater than zero. The symbol for the factorial function is an exclamation mark after a number, like this: 5!

A recursive function is a function that calls itself multiple times until a particular condition is satisfied. Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the number : 7

Output

The desired output would be −

The factorial of 7 is 5040

Algorithm

Step 1 - START
Step 2 - Declare an integer values namely ‘my_input’ and a long value namely ‘my_result’
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘factorial’ is defined which takes an integer as input and returns the product of the input and its previous number until the input value is reduced to 1.
Step 5 - The recursive function is called and the value ‘my_input’ is passed to it. Store the return value
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class Factorial {
   public static void main(String[] args) {
      int my_input ;
      long my_result;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      my_result = factorial(my_input);
      System.out.println("The factorial of " + my_input + " is " + my_result);
   }
   public static long factorial(int my_input){
      if (my_input >= 1)
         return my_input * factorial(my_input - 1);
      else
         return 1;
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number : 7
The factorial of 7 is 5040

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Factorial {
   public static void main(String[] args) {
      int my_input ;
      long my_result;
      my_input = 7;
      System.out.println("The number is defined as " +my_input);
      my_result = factorial(my_input);
      System.out.println("The factorial of " + my_input + " is " + my_result);
   }
   public static long factorial(int my_input){
      if (my_input >= 1)
         return my_input * factorial(my_input - 1);
      else
         return 1;
   }
}

Output

The number is defined as 7
The factorial of 7 is 5040
raja
Updated on 22-Feb-2022 09:43:15

Advertisements