Java Program to find Factorial of a Number

Write a Java Program to find the Factorial of a number using For Loop, While Loop, Functions, and Recursion. The Factorial of a number is the product of all the numbers less than or equal to that number & greater than 0. It is denoted with a (!) symbol. 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

Java Factorial Program using For Loop

This program allows the user to enter any integer value. By using this value, this Java program finds the Factorial of a number using the For Loop.

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 Java factorial program, 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 declare the output as a long variable. When 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 assign 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 terminate when the condition fails.

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

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 to the Increment and Decrement Operators article to understand the ++ notation.

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

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

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

  • For loop Condition (3 <= 4) is True
  • Fctl = 2 *3 = 6
  • i will become 4.

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

  • Condition inside the For loop (4 <= 4) is True
  • Fctl = 6 * 4 = 24
  • i will become 5.

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

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

System.out.format("\nFactorial of %d = %d\n", Number, Fctl);

Java Program to find the Factorial of a Number using While Loop

This program allows the user to enter any integer value. Using this value, this Java program will find the 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.print("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 replaced the For loop in the above Java factorial program example with the While loop. If you do not understand the While Loop, please refer to the Java article.

The Java factorial of a number program using a while loop output.

Java program to find Factorial of a Number using while loop

Java Program to find the Factorial of a Number 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 the Factorial of a given number using the For Loop and functions.

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.print("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;
 }
}
Java program to find Factorial of a Number using Functions

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 the 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 a value of type long.

Java Program to find the Factorial of a Number using Recursion

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

In this Java program, we are dividing the code using Object-Oriented Programming. To do this, we will first create a class with 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.print("Please Enter any number : ");
 n = sc.nextInt();
 
 FacNum fn = new FacNum();
 Fact = fn.FctN(n); 
 System.out.format("Factorial of %d = %d", n, Fact);
 }
}

Otherwise, you can write the whole code in a single file.

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

		 System.out.print("Please Enter any number : ");
		 n = sc.nextInt();
		 
		 Fact = FaN(n); 
		 System.out.format("Factorial of %d = %d", n, Fact);
	 }
	 
	 public static long FaN(int n)  {
		 if (n == 0 || n == 1)  {
			 return 1;
		}
		else {
			return n * FaN (n -1);
		}
	 }
}

The Java factorial of a number using recursive functions program output.

Java program to find Factorial of a Number using Recursion

In this Java factorial program using Recursion, we defined a function within this class. The following function will accept integer values as parameter values 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, the function recursively returns n * (n -1).

User Entered Value in this Factorial Program = 8

First, the 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.