Factorial Program in Java

Write a Factorial Program in Java using For Loop, While Loop, Functions, and Recursion. The Factorial of number is the product of all the numbers less than or equal to that number & greater than 0. It is denoted with a (!) symbol.

Java Factorial Program using For Loop

This Java factorial program allows the user to enter any integer value. By using this value, this Java program finds Factorial of a number using the For Loop. The mathematical representation of this is as shown below:

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

For example, 5! is represented as = 5 * 4 * 3 * 2 * 1 = 120

import java.util.Scanner;

public class Example {	
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int i, Number; 
		long Fctl = 1;
		sc = new Scanner(System.in);	
		
		System.out.println(" Please Enter any number to Find : ");
		Number = sc.nextInt();
		
		for (i = 1; i <= Number; i++)  {
			Fctl = Fctl * i;
		}
		System.out.format("\nFactorial of %d = %d\n", Number, Fctl);
	}
}
Factorial Program in Java 1

In this factorial program in Java, First, We declared two integer variables i and number. We also declared one long variable and the assigned value of 1.

Although we are calculating the factorial of an integer variable, we declared output as a long variable. Because, when we are writing a Java program for calculating the factorial for large integers, the result will cross the integer limit.

The following System.out.println statement will ask the user to enter his/her own integer value to calculate the factorial. Next, we are assigning the user entered value to the Number variable

Within the For Loop, We initialized the integer I value to 1 and also (i <= Number) condition will help the loop to terminate when the condition fails.

Let us see the working principle of this Java factorial program for loop in iteration wise. From the above screenshot, you can observe that the user entered the value: Number = 4 and as we know i = 10.

Java factorial First Iteration

  • Condition inside the For loop (1 <= 4) is True. So, the program will start executing statements inside the for loop
  • Fctl = Fctl * i => 1 *1 = 1
  • Lastly, i will increment to 1. It means i will become 2. Please refer Increment and Decrement Operators article to understand the ++ notation.

Second Iteration: From the first iteration, i became 2 and Fact = 1

  • Condition inside the For loop (2 <= 4) is True. So, the Java Factorial program will start executing statements inside the for loop
  • Fctl = 1 *2 = 2
  • Lastly, i will increment to 1. It means i will become 3.

Third Iteration: From the second iteration, i = 3 and Fact = 2

  • For loop Condition (3 <= 4) is True
  • Fctl = 2 *3 = 6
  • Lastly, i will increment to 1. It means i will become 4.

Fourth Iteration: From the third iteration of Java program, i = 4 and Fctl = 6

  • Condition inside the For loop (4 <= 4) is True
  • Fctl = 6 * 4 = 24
  • Lastly, i incremented to 1. It means i will become 5.

Fifth Iteration: Condition inside the For loop (4 <= 4) is False so, it will be Terminated.

Last System.out.format statement will print the output of the user entered integer. Within the statement, the first %d refers to Number, and the second %d refers to the actual value.

Factorial Program in Java using While Loop

This factorial program in java allows the user to enter any integer value. By using this value, this Java program will find Factorial of a number using the While Loop

package FrequentPrograms;

import java.util.Scanner;
public class Example {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i = 1, num; 
		long fa = 1;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter any Value : ");
		num = sc.nextInt();
		
		while (i <= num)  {
			fa = fa * i;
			i++;
		}
		System.out.format(" The Result of %d = %d", num, fa);
	}
}

We just replaced the For loop in the above Java factorial program example with the While loop. If you do not understand the While Loop, then please refer to Java article here: While Loop.

Java factorial using a while loop output

 Please Enter any number : 
6
 The Result of 6 = 720

Factorial Program in Java using Functions

This Java program allows the user to enter any integer value. User entered value will be passed to the Method we created. Within this User defined function, this java program will find Factorial of a given number using the For Loop

package FrequentPrograms;

import java.util.Scanner;

public class Example {
 private static Scanner sc;
 private static long Fact = 1;
 public static void main(String[] args) {
 int Num; 
 sc = new Scanner(System.in); 

 System.out.println(" Please Enter any num : ");
 Num = sc.nextInt();
 
 Fact = FaN(Num); 
 System.out.format(" Factorial of %d = %d", Num, Fact);
 }
 
 public static long FaN (int Num)  {
 int i;
 for (i = 1; i <= Num; i++)  {
 Fact = Fact * i;
 }
 return Fact;
 }
}
 Please Enter any num : 
7
 Factorial of 7 = 5040

In this java factorial program using functions example, We assigned the function return value to a long variable. Because the FaN(Num) will return the long value as output.

Fact = FaN(Num);

When the compiler reaches to FaN(Num) line in the main() program, the Jcompiler will immediately jump to below function:

public static long FaN (int Num)  {

The last line ends with a return Statement. It means every time we call the FaN( ) function it will return value of type long.

Java Factorial Program using Recursion

This factorial program in java using Recursion allows the user to enter any integer value. User entered value will be passed to the Function we created. Within this User defined function, this program will find for a number Recursively.

In this Java program, we are dividing the code using the Object-Oriented Programming. To do this, first, we will create a class that holds a method to reverse an integer recursively.

package FrequentPrograms;

public class FacNum {
	public long FctN (int n)  {
		if (n == 0 || n == 1)  {
			return 1;
		}
		else  {
			return n * FctN (n -1);
		}
	}
}

Within the Main program, we will create an instance of the above-specified class and call the methods

package FrequentPrograms;

import java.util.Scanner;

public class Example {
 private static Scanner sc;
 public static void main(String[] args) {
 int n; 
 long Fact = 1;
 sc = new Scanner(System.in); 

 System.out.println(" Please Enter any number : ");
 n = sc.nextInt();
 
 FacNum fn = new FacNum();
 Fact = fn.FctN(n); 
 System.out.format(" Factorial of %d = %d", n, Fact);
 }
}

Java factorial using recursive functions output

 Please Enter any number : 
8
 Factorial of 8 = 40320

FacNum Class Analysis:

In this java factorial program using Recursion, Within this class, we defined a function. The following function will accept integer values as parameter value and return an integer value

public long FctN (int n)  {

Within the user-defined function, We used the If Else Statement to check whether the Given Number is Equal to 0 or Equal to 1.

  • If the condition is TRUE, then the function will return 1.
  • If the condition is False, then the function will return Number * (Number -1) recursively.

User Entered Value in this Factorial Program in Java = 8

First If statement fails So,

Fact = Number * FctN(Number -1);

= 8 * FctN (8 -1)

= 8 * FctN (7)

Fact = 8 * 7 * FctN (6)

= 8 * 7 * 6 * FctN (5)

= 8 * 7 * 6 * 5 * FctN (4)

Fact = 8 * 7 * 6 * 5 * 4 * FctN (3)

= 8 * 7 * 6 * 5 * 4 * 3 * FctN (2)

= 8 * 7 * 6 * 5 * 4 * 3 * 2 * FctN (1)

Fact = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1

= 40320.