Calculate factorial of a number in Java using recursion

Carvia Tech | December 25, 2019 | 2 min read | 172 views | Java Coding Challenges

Factorial of a number is calculated using below formula:

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

the factorial of a number is calculated by formula number * (number -1) till zero and since the value of factorial zero is 1, it acts as a base case in the recursive version of the factorial method.

Recursive approach

Recursive function to calculate factorial
public class Factorial {
    public long factorial(long n) {
        if (n == 1)
            return 1;
        else {
            return n * factorial(n - 1);

Iterative approach

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

Factorial using Java 8 Streams

Java 8 streams with reduction method can be used to calculate factorial of a number.

java 8 Streams for factorial calculation

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. Check if the given number is Armstrong Number in Java
  2. Check a number is Prime: Java Coding Problem
  3. Create anagram buckets from a given input array of words
  4. How to reverse a number in Java
  5. pangram checker in java
  6. How to check if the given number is palindrome in Java
  7. Java program to check if two strings are anagrams
See all articles in Java Coding Challenges
Top articles in this category:
  1. Top 15 coding problems for SDET Java Interviews
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: Rest Assured Interview Questions
  4. SDET: JUnit interview questions for automation engineer
  5. Check if the given number is Armstrong Number in Java
  6. Write a program to reverse a string using recursion in Java
  7. Calculate Fibonacci Series in Java

Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info