Factorial

  • Factorial of a non-negative integer n is the multiplication of all positive integers smaller than or equal to n. Example : factorial of 5 is 5 * 4 * 3 * 2 * 1 which is 120.
  • Factorial is represented by a number with ‘!’ mark at the end.
  • The notation n! for factorials was introduced by the French mathematician, Christian Kramp in 1808. Kramp had given name ‘faculty’ and it was Arbogast who first used the word ‘factorial‘(originally French: factorielle) in 1800
  • In mathematics, factorial is widely used in Combinations and Permutations.

There are different ways to calculate factorial of number in Java.

Following four programs illustrate Java program to calculate factorial of number using loop, iteration, recursion & ternary operator.

1) Simple Java program to calculate Factorial of a number using loop :

public class FactorialLoop {

	 public static void main(String args[]){  
		    int num_to_find,fact=1; 		  
			Scanner sc= new Scanner(System.in);  
			System.out.print("Enter the number to find Factorial of : ");  
			num_to_find=sc.nextInt();  
			System.out.print("Entered number is : " + num_to_find + "\n");  
			
			for(int i=1;i<=num_to_find;i++){    
			      fact=fact*i;    
			}    
			  
		    System.out.println("Factorial of "+num_to_find+" is: "+fact);    
		 }  
}

Output :

Enter the number to find Factorial of : 4
Entered number is : 4
Factorial of 4 is: 24

2) Java program to calculate Factorial of a number using Iteration :

import java.util.Scanner;

public class FactorialIteration {

	  static int factorial(int num) {
	      int result = 1;
	      for (int i = 1; i <= num; i++) {
	    	  result = result * i;
	      }
	      return result;
	   }
	   public static void main(String[] args) {

		   int fact=1;  
		   int num_to_find;   
		   Scanner sc= new Scanner(System.in);  
		   System.out.print("Enter the number to find Factorial of : ");  
		   num_to_find=sc.nextInt();  
		   System.out.print("Entered number is : " + num_to_find + "\n");  
		   int result = factorial(num_to_find);
	       System.out.println("The factorial of " + num_to_find + " is " + result);
	   }
}


Output :

Enter the number to find Factorial of : 7
Entered number is : 7
The factorial of 7 is 5040

3) Java program to calculate Factorial of a number using recursion :

public class FactorialRecursion {

	static int factorial(int num){    
		  if (num == 0)    
		    return 1;    
		  else    
		    return(num * factorial(num-1));    
		 }    
	
		 public static void main(String args[]){  
		  int fact=1;  
		  int num_to_find;   
		  Scanner sc= new Scanner(System.in);  
		  System.out.print("Enter the number to find Factorial of : ");  
		  num_to_find=sc.nextInt();  
		  System.out.print("Entered number is : " + num_to_find + "\n");  

		  fact = factorial(num_to_find);   
		  System.out.println("Factorial of "+num_to_find+" is: "+fact);    
		 }  
}


Output :

Enter the number to find Factorial of : 5
Entered number is : 5
Factorial of 5 is: 120

4) Java program to calculate Factorial of a number using Ternary operator (One line solution) :

import java.util.Scanner;

public class FactorialTernary {

	int factorial(int num) 
	{ 
		return (num == 1 || num == 0) ? 1: num * factorial(num - 1); 
	} 

	public static void main(String args[]) 
	{ 
		int num_to_find;   
		Scanner sc= new Scanner(System.in);  
		System.out.print("Enter the number to find Factorial of : ");  
		num_to_find=sc.nextInt();  
		System.out.print("Entered number is : " + num_to_find + "\n");  

		FactorialTernary obj = new FactorialTernary(); 
		
		System.out.print("Factorial of " + num_to_find + " is " + obj.factorial(num_to_find)); 
	} 
}


Output:

Enter the number to find Factorial of : 6
Entered number is : 6
Factorial of 6 is 720
Scroll to Top