Java Program to find Factorial of a number

Upasana | October 17, 2020 | 2 min read | 382 views | Java Coding Challenges


Factorial of a number is calculated using below mathematical formula:

factorial (n) = n x (n-1) x (n-2) ... 1

In Java, we can either use iterative approach or recursive approach to calculate the factorial. We will discuss both of them in this article.

Recursive approach

The factorial of a number can be expressed by this recursive function:

factorial (n) = n x factorial (n-1)

till n becomes 1 i.e. till factorial of 0 (which is one).

Recursive function to calculate factorial
public class Factorial {
    public long factorial(long n) {
        if (n == 1)     (1)
            return 1;
        else {
            return n * factorial(n - 1);    (2)
        }
    }
}
1 Base condition for recursive method. Factorial of Zero is One.
2 We multiply number with factorial of (number - 1) to get factorial of a number

Iterative approach

Iterative approach is bit simple, where we multiply all numbers from 1 to n and return the result.

Iterative approach for factorial
class Scratch {
    public long factorial(long number) {
        long result = 1L;
        while (number != 0) {
            result = result * number;
            number--;
        }
        return result;
    }
}

Factorial using Java 8 Streams

Java 8 streams with reduction method can be used to calculate factorial of a number. This is a iterative approach, but instead of loops we are using streams.

java 8 Streams for factorial calculation
import java.util.stream.LongStream;

class Scratch {
    public static long factorialStreams(long n) {
        return LongStream.rangeClosed(2, n)
                .reduce(1, (long a, long b) -> a * b);
    }
}
  • LongStream.rangeClosed(2, n) method creates a Stream of longs from [2 to n].

  • Lambda function supplied for reduce (a,b) → a * b will multiply each pair of a and b and return the result. The result then carries over to a for the next round. The value of 1 is used in the reduction method as the starting value for variable a for the first iteration.

Handling very large numbers

Here we are using long to hold the result of multiplication, which can quickly overflow due to maximum limit an long can hold. We can improve the above two programs to use BigInteger as discussed in the below article:


Java Coding Challenges:
  1. Reverse position of words in a string using recursion
  2. Check if the given string is palindrome
  3. Find two numbers of which the product is maximum in an array
  4. Prime number checker in Java
  5. Create anagram buckets from a given input array of words
  6. Anagrams string checker in Java
  7. Reverse a string using recursion in Java
See all articles in Java Coding Challenges
Top articles in this category:
  1. Java Coding Problems for SDET Automation Engineer
  2. Find two numbers of which the product is maximum in an array
  3. 50 Java Interview Questions for SDET Automation Engineer
  4. How to reverse a number in Java
  5. Armstrong Number in Java
  6. Prime number checker in Java
  7. Find longest non-repeating substring from a given string in Java

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.