Factorial Program in Java

In this article, we will show you How to write a Factorial Program in Java Programming Language using For Loop, While Loop, Functions and Recursion.

Java Factorial Program Definition

The Factorial of any number is the product of all the numbers that are less than or equal to that number and greater than 0 and it is denoted with the symbol (!). The mathematical representation of the Factorial is as shown below:

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

int i, Number;
long Factorial = 1;

NOTE: Although we are calculating the factorial of an integer variable we declared output (Factorial) as a long variable. Because, When we are writing a Java program for calculating the factorial for large integers, the result will definitely 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

System.out.println(" Please Enter any number to Find Factorial: ");
Number = sc.nextInt();

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.

for (i = 1; i <= Number; i++) {
Factorial = Factorial * i;
}

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

First Iteration

Condition inside the For loop (1 <= 4) is True. So, the program will start executing statements inside the for loop

We just replaced the For loop in the above Java factorial program example with the While loop. If you don’t understand the While Loop then please refer While Loop article here: Java While Loop

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 factorial program will find Factorial of a given number using the For Loop

This java factorial program using functions example, We assigned the function return value to long variable because the FactorialNumber(Number); function will return the long value as output

Factorial = FactorialNumber(Number);

When the compiler reaches to FactorialNumber(Number) line in main() program, the compiler will immediately jump to below function:

public static long FactorialNumber (int Number) {

We already explained the LOGIC in above Java factorial program example.

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

NOTE: If we create a function with Void then there is no need to return any value but, if we declared a function with any data type (int, float etc) then we have return something out from the function.

Java Factorial Program using Recursion

This Java factorial program 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 Factorial of 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 which holds a method to reverse an integer recursively.