Find the factorial of a number using recursion in Java

Factorial Number

Factorial says to multiply all whole numbers from the chosen number down to 1.

The symbol is “!”

The value of 0! is 1, according to the convention for an empty product.

For Example :

6!=6x5x4x3x2x1 =720

Mathematically, the formula for the factorial is as follows. If n is an integer greater than or equal to 1, then

n ! = n ( n – 1)( n – 2)( n – 3) … (3)(2)(1)

The factorial values for negative integers are not defined.

Factorials are used in many areas of mathematics, but particularly in Combination and permutation.

factorial of number

Implementation of Factorial number Using recursion

Firstly we import the scanner class from util package for user input

call the function for calculating the factorial of a number.

Then we define  the function and call recursively

Code of Factorial of a Number using recursion in Java

     import java.util.Scanner;
     class Main

           public static void main(String args[]){
          //Scanner object for take the user input
                    Scanner scanner = new Scanner(;
                   System.out.println("Enter the number:");
           //Stored the entered value in variable
                     int num = scanner.nextInt();
//Called the user defined function fact
              int factorial = fact(num);
                System.out.println("Factorial of entered number is: "+factorial);
      static int fact(int n)
       int output;
      return 1;
     //Recursion: Function calling itself!!
     output = fact(n-1)* n;
     return output;
enter the number 


Factorial of entered number is: 5040