Factorial Program in Java

Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to be calculated is represented by p!.It is pronounced as p factorial. In this section, we will create a Java program to calculate the factorial of a number using iterative and recursive approach.

Factorial of any number is the multiplication of numbers from that particular number to 1 either in increasing or in decreasing order. In general terms, we can represent the factorial of a number as follows:

For example:

3! = 3 × 2 × 1 or 1 × 2 × 3 = 6

4! = 4 × 3 × 2 × 1 or 1 × 2 × 3 × 4 = 24

Note: As per standard mathematical definition, the factorial of any negative number cannot be found. Also, 0! is always 1.

Let’s write a Java program that calculates the factorial of a number using Java for loop.

Iterative Approach

Filename: FactorialProgramExample.java

Output:

In the above example, the variable factorial initialized to 1. We have used the Java for loop to find the factorial of a number. The loop executes until the condition j <= no becomes false. After each iteration, the variable j increments by 1. The for loop executes in the following order:

First iteration: The variables factorial = 1, and j = 1.

factorial = factorial * j => factorial = 1×1 = 1

Second iteration: After the first iteration, the value of factorial become 1 and the value of j incremented by 1 i.e. 2.  Hence, factorial = 1, and j = 2.

factorial = factorial * j => factorial = 1×2 = 2

Third iteration: After the second iteration, the value of factorial become 2 and the value of j incremented by 1 i.e. 3. Hence, factorial = 2, and j = 3.

factorial = factorial * j => factorial = 2×3 = 6

Fourth iteration: After the third iteration, the value of factorial become 6 and the value of j incremented by 1 i.e. 4. Hence, factorial = 6, and j = 4.

factorial = factorial * j => factorial = 6×4 = 24

Fifth iteration: After the fourth iteration, the value of factorial become 24 and the value of j incremented by 1 i.e. 5. Hence, factorial = 24, and j = 5.

factorial = factorial * j => factorial = 24×5 = 120

Sixth iteration: After the fifth iteration, the value of factorial become 120 and the value of j incremented by 1 i.e. 6. Hence, factorial = 120, and j = 6.

factorial = factorial * j => factorial = 120×6 = 720

After the sixth iteration, the value of factorial become 720 and the value of j incremented by 1 i.e. 7. Now the condition j <= no becomes false. The loop will terminate and thus, we get 6! = 720.

Recursive Approach

We can also use the recursive approach to find the factorial. The demonstration is given below.

Filename: FactorialProgramExample1.java

Output:

Explanation: Here, the recursive approach worked because we can also write 6! as 6 × 5! and 5! as 5 × 4! and so on. The findFactorial() method finds the factorial of the number passed to its argument. So, if we pass 6 in the method argument, then the method will find the factorial of 6, and if 5 is passed, the method will find the factorial of 5. Therefore, we can say

findFactorial(6) = 6 × findFactorial(5)

findFactorial(5) = 5 × findFactorial(4)

findFactorial(4) = 4 × findFactorial(3)

findFactorial(3) = 3 × findFactorial(2)

findFactorial(2) = 2 × findFactorial(1)

findFactorial(1) = 1

Combining all the above, we get:

findFactorial(6) = 6 × 5 × 4 × 3 × 2 × 1 = 720.