Factorial In Java, Factorial Using Recursion, Factorial Lambda Expression

  In this java program example, we will learn to create a program to find factorial of a number, factorial number is a product of all positive descending integers of a given 'N' number. In math factorial is denoted by n number represent as n!.

Factorial Number

Factorioal number represented as n!. Factorial number is calculate as given:
suppose, n = 5 and you need a program to find the factorial of n!

n = 5 X 4!
n = 5 x 4 x 3!
n = 5 x 4 x 3 x 2!;
n = 5 x 4 x 3 x 2 X 1!
n = 5 x 4 x 3 x 2 X 1 x 0! [ Factorial of 0! is 1. ]
n = 5 x 4 x 3 x 2 X 1 x 1
n = 120

The factorial of n=5 is 120.
Let's take a look in java prgramming

Java Factorial Number

In Java, there are many ways to write a program to find a factorial of given number. Let's create a program in different ways as :

  1. Factorial using Loop (Iterative approach)
  2. Factorial using Recursion
  3. Factorial using Lambda Expression

Factorial using Loop (Iterative approach) in Java

An iterative approach is simple and easy to implement for beginners, it multiple the value at every iteration with the previous value. There is a different approach to find factorial as shown below:

  1. Factorial using For Loop
  2. Factorial using While Loop
  3. Factorial using do While Loop

Factorial using For Loop

																
public class FactorialForLoop{
    
     public static void main(String []args){
        int  n = 5 ;  // given number to calculate factorial 
        int fact=1;  // create variable set to the 1(because multiple of 0 will be 0.) to store the results.
        
        /*** start with n = 5 and decrement it by 1 until the value of i is equal to 1.
         * because the factorial of 0! is 1.
         *
         */
        for(int i=n;i>0;i--) {
            
            fact = fact * i;  // multiply as 5 x 4 x 3 x 2 x 1 
        }
        System.out.println("The Factorial(using For Loop) of "+n+" is "+fact); // print results
     }
}													
													
												

Output :

														
The Factorial(using For Loop) of 5 is 120											
													
												

Factorial using While Loop

																
public class FactorialWhileLoop{
    
     public static void main(String []args){
        int  n = 5 ;  // given number to calculate factorial 
        int fact=1;  // create variable set to the 1(because multiple of 0 will be 0.) to store the results.
        
        /*** start with n = 5 and decrement it by 1 until the value of i is equal to 1.
         * because the factorial of 0! is 1.
         *
         */
        int i=n; // initialize i
        while(i>0) { 
            
            fact = fact * i;  // multiply as 5 x 4 x 3 x 2 x 1 
            
            i--; //decrement i by 1
        }
        System.out.println("The Factorial (using While Loop) of "+n+" is "+fact); // print results
     }
}											
													
												

Output :

														
The Factorial (using While Loop) of 5 is 120												
													
												

Factorial using Do While Loop

																
public class FactorialDoWhileLoop{
    
     public static void main(String []args){
        int  n = 5 ;  // given number to calculate factorial 
        int fact=1;  // create variable set to the 1(because multiple of 0 will be 0.) to store the results.
        
        /*** start with n = 5 and decrement it by 1 until the value of i is equal to 1.
         * because the factorial of 0! is 1.
         *
         */
        int i=n; // initialize i
        do{ 
            
            fact = fact * i;  // multiply as 5 x 4 x 3 x 2 x 1 
            
            i--; //decrement i by 1
        }while(i>0);
        
        System.out.println("The Factorial (using Do While Loop) of "+n+" is "+fact); // print results
     }
}											
													
												

Output :

														
The Factorial (using Do While Loop) of 5 is 120												
													
												

Factorial using Recursion in Java

In this, a function that calls itself is called the recursive function. we use the recursive function to calculate the function until the exit condition executed.

																
public class FactorialForLoop{
    /***
     * Note : calculateFact() function is used to calcuate the factorial
     * @param: int (n)
     * return int (calculated value)
     * 
     **/
     public static int calculateFact(int n){ // create a static function by which we don't need to create a object of class.
        
         if(n == 0){
             return 1; // factorial of 0 is always 1 so return 1.
         }
         else{
             return n*calculateFact(n-1); // recursive call and multiply as 5 x 4 x 3 x 2 x 1 
         }
         
     }
    
     public static void main(String []args){
        int  n = 5 ;  // given number to calculate factorial 
        int fact;  // create variable to store the results.
        
        fact  =  calculateFact(n); // call function and pass n value to calcuate the factorial.
      
        System.out.println("The Factorial(using For Recursion) of "+n+" is "+fact); // print results
     }
}										
													
												

Output :

														
The Factorial(using For Recursion) of 5 is 120											
													
												

Factorial using Lambda Expression in Java

Another way to create a factorial program, that is a little complex than the previous program, this factorial program uses the streams of java 8. LongStream.rangeClosed(2, n) method creates a Stream of range from 2 to n. Lambda function called the reduce (x,y) → x * y, that will multiply each pair of x and y and return the result.

																
import java.util.stream.LongStream;
 
public class FactorialForLoop{
    /***
     * Note : factorialStreams() long stream function is used to calcuate the factorial
     * @param: long (n)
     * return long (calculated value)
     * 
     **/
    public static long factorialStreams(long n) {
        /* it create a stream from the range 2 to n */
        return LongStream.rangeClosed(2, n).reduce(1, (long x, long y) -> x * y);
        
    }
    
     public static void main(String []args){
        long  n = 5 ;  // given number to calculate factorial 
        long fact;  // create variable to store the results.

       
        fact  = factorialStreams(n);
        System.out.println("The Factorial(using For Lambda Expression) of "+n+" is "+fact); // print results
     }
}											
													
												

Output :

														

The Factorial(using For Lambda Expression) of 5 is 120										
													
												
Announcement
Advertisement