Java program – Factorial using recursion


import java.util.*;    
class factRec
{  
        int factorial(int n) //5 ! =5*4*3*2*1 =120 
     {    
      if (n == 0)       // 0!=1 so when given number is 0, it returns 1
        return 1;    
      else          // finding factorial by n * factorial(n-1)
        return(n * factorial(n-1));    
     }    
     public static void main(String args[])
     {  
         Scanner sc=new Scanner(System.in);
         factRec fr=new factRec();   //creating object fr
         System.out.println("Enter the number to find the factorial");
         int number=sc.nextInt();//It is the number to calculate factorial
         int fact=1;  
         fact = fr.factorial(number);   //method invocation factorial(number)
                                        // calling recursive method
         System.out.println("Factorial of "+number+" is: "+fact);    
     }  
    }  

Output:

Enter the number to find the factorial
5
Factorial of 5 is: 120
Enter the number to find the factorial
8
Factorial of 8 is: 40320

Factorial using loop and using recursive method


Factorial of a number is the product of all descending numbers from the given number n. 

Example 5!  Means 5*4*3*2*1

The product of the descending numbers from 5 is 120.

Let’s look, can write a program using loop and recursion.

Looping statements 3

1. for loop

2. while loop

3. do while loop

I. Using for  loop

class FactorialLoop1
{  
 public static void main(String args[])
{  
  int i,fact=1;  
  int number=5; // to find the factorial of value that assigned to the variable number
  for(i=1;i<=number;i++)
{    
      fact=fact*i;    
  }    
  System.out.println("Factorial of the given number "+number+" is: "+fact);    
 }  
}  

Output

Factorial of the given number 5 is : 120.

2. Using while loop

class FactorialLoop2
{  
 public static void main(String args[])
{  
  int i=1, fact=1;
  int number=5; // to find the factorial of value that assigned to the variable number
while( i<=number)
{    
      fact=fact*i;    
      i++;
  }    
  System.out.println("Factorial of the given number "+number+" is: "+fact);    
 }  
} 

Output

Factorial of the given number 5 is:  120.

III. Using do while loop

class FactorialLoop3
{  
 public static void main(String args[])
{  
  int i=1, fact=1;
  int number=5; // to find the factorial of value that assigned to the variable number
do
{    
      fact=fact*i;    
      i++;
  }   while( i<=number);
  System.out.println("Factorial of the given number "+number+" is: "+fact);    
 }  
} 

Output

Factorial of the given number 5 is : 120.

Using Recursion

class FactorialUsingRecursion 
 static int factorial(int n)
{
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  
  int  fact=1;  
  int number=5; //It is the number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of the given number"+number+" is: "+fact);    
 }  
}  

Output

Factorial of the given number is: 120