Home » Programming » Java Interview Programs » Java program to find factorial of a number by different ways

Java program to find factorial of a number by different ways

In this article let us see how to write a Java program to find a factorial of a given number by using for loop, while loop, and recursion. Factorial of a number is a product of all the subsequent positive lowest numbers, the product starts from the number that we want to find factorial and up to 1.

We can find the factorial of a number by using the below formula.

n! = n * (n-1) * (n-2) * (n-3) * ......... * 1

For example, the value of 5! = 5*4*3*2*1 = 120.

We can derive many formulas from the above one like below.

n! = n * (n-1)! (or) n! = n * (n-1) * (n-2)!.

For example, We can write 5! = 5 * 4! (or) 5!= 5 * 4 * 3!.

Note:

  1. We can't find the factorial of a negative number. But the mathematicians can extend the factorials for complex numbers through the Gamma function.
  2. The value of 0! (Zero Factorial) is 1.

Usage:

Generally, factorial is used in Permutation, Combination, and Probability in Mathematics.

Java program to find factorial by using for loop

public class FactorialProgram {	
        //using for loop	
	public static int findFactorial(int n){
		int result =1;
		for(int i=n; i>0; i--){
			result = result*i ;
		}
		return result;
	}	
	public static void main(String[] args){
		int n = 6;
		if(n>=0)
		System.out.println("The value of "+n+" factorial is "+findFactorial(n));
		else
		System.out.println("We can't find the factorial of negative numbers");
	}
}

Output

The value of 6 factorial is 720.

Java program to find factorial by using while loop

public class FactorialProgram {	
	//using while loop	
	public static int findFactorial(int n){
		int result =1;
		while(n>0){
			result = result*n;
			n--;
		}
		return result;
	}	
	public static void main(String[] args){
		int n = 10;
		if(n>=0)
		System.out.println("The value of "+n+" factorial is "+findFactorial(n));
		else
		System.out.println("We can't find the factorial of negative numbers");
	}
}

Output

The value of 10 factorial is 3628800.

Java program to find factorial by using recursion function

public class FactorialProgram {	
	//using recursion
	static int result =1;
	public static int findFactorial(int n){		
		if(n>0){
			result = result *n;
			findFactorial(n-1);
		}
		else if(n==0)
			return result;
		
		return result;
	}	
	public static void main(String[] args){
		int n = 8;
		if(n>=0)
		System.out.println("The value of "+n+" factorial is "+findFactorial(n));
		else
		System.out.println("We can't find the factorial of negative numbers");
	}
}

Output

The value of 8 factorial is 40320.

As of now, we have seen different ways to find the factorial of a number using the java programs. Now let us see some examples of its usages.

Usage of factorial in Permutation and Combination in Mathematics

Let's take the below sample problem from permutation to understand.

Problem: In how many ways can first and second place be awarded to 10 people?.

Solution:

The formula to find the permutation is below.

permutation-formula

Here, n = total number of objects, r = number of selected objects.

So we have to award two places first and second to out of 10 people. Here n = 10 and r = 2. Apply these values in the formula to find the answer.

10P2 = 10! / (10-2)! = 10! / 8! = (10 * 9 * 8 * 7 * 6 * 5* 4 * 3 * 2 * 1) / ( 8 * 7 * 6 * 5* 4 * 3 * 2 * 1 ) = 90.

There are 90 ways to award first and second places to 10 people.

Let's take the below sample problem from combination to understand.

Problem: In how many ways does the coach form a team of 3 players from among 5 players?.

Solution:

The formula to find the combination is below.

combination-formula

Here, n = total number of objects in the set, r = number of selected objects from the set.

So the coach has to form a team of 3 players from among 5 players. Here n = 5 and r = 3. Apply these values in the formula to find the answer.

5C3 = 5!/ ((5-3)!*3!) = 5! / (2! * 3!) = 10.

There are 10 ways to form a team of 3 players from among 5 players.

The difference between permutation and combination is the order of arrangement. In permutation order of arrangement is matter but in combination order of arrangement doesn't matter. For example, In a permutation, the arrangement XYZ and XZY are different. But, in combination, the arrangements XYZ and XZY are the same because the order is not important.

Cool!. I hope you understood how to write a java program to find factorial for the given number and the usage of factorial in permutation and combination in mathematics. Thanks!. Keep Reading!.

Leave a Reply

Your email address will not be published. Required fields are marked *