0
0
JavaProgramBeginner · 2 min read

Java Program to Find Factorial Using Recursion

You can find the factorial of a number in Java using recursion by defining a method like int factorial(int n) { return (n == 0) ? 1 : n * factorial(n - 1); } which calls itself until it reaches zero.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input1
OutputFactorial of 1 is 1
🧠

How to Think About It

To find the factorial of a number using recursion, think of the problem as multiplying the number by the factorial of the number just before it. Keep doing this until you reach 0, where the factorial is 1 by definition. This way, the function calls itself with smaller numbers until it reaches the base case.
📐

Algorithm

1
Get the input number n
2
Check if n is 0; if yes, return 1 as factorial of 0 is 1
3
Otherwise, call the factorial function recursively with n-1
4
Multiply n by the result of the recursive call
5
Return the multiplication result
💻

Code

java
public class Factorial {
    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static void main(String[] args) {
        int number = 5;
        System.out.println("Factorial of " + number + " is " + factorial(number));
    }
}
Output
Factorial of 5 is 120
🔍

Dry Run

Let's trace factorial(5) through the code

1

Call factorial(5)

Since 5 != 0, return 5 * factorial(4)

2

Call factorial(4)

Since 4 != 0, return 4 * factorial(3)

3

Call factorial(3)

Since 3 != 0, return 3 * factorial(2)

4

Call factorial(2)

Since 2 != 0, return 2 * factorial(1)

5

Call factorial(1)

Since 1 != 0, return 1 * factorial(0)

6

Call factorial(0)

Since 0 == 0, return 1

7

Return values back up

factorial(1) = 1 * 1 = 1 factorial(2) = 2 * 1 = 2 factorial(3) = 3 * 2 = 6 factorial(4) = 4 * 6 = 24 factorial(5) = 5 * 24 = 120

CallReturn Value
factorial(0)1
factorial(1)1
factorial(2)2
factorial(3)6
factorial(4)24
factorial(5)120
💡

Why This Works

Step 1: Base Case

The recursion stops when n == 0, returning 1 because factorial of zero is defined as 1.

Step 2: Recursive Call

For any other number, the function calls itself with n - 1, breaking the problem into smaller parts.

Step 3: Multiplication

Each call multiplies the current number n by the factorial of n - 1, building the final result as the calls return.

🔄

Alternative Approaches

Iterative approach
java
public class Factorial {
    public static int factorial(int n) {
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }

    public static void main(String[] args) {
        int number = 5;
        System.out.println("Factorial of " + number + " is " + factorial(number));
    }
}
This uses a loop instead of recursion, which can be easier to understand and avoids stack overflow for large numbers.
Using Java Streams
java
import java.util.stream.IntStream;

public class Factorial {
    public static int factorial(int n) {
        return IntStream.rangeClosed(1, n).reduce(1, (a, b) -> a * b);
    }

    public static void main(String[] args) {
        int number = 5;
        System.out.println("Factorial of " + number + " is " + factorial(number));
    }
}
This uses Java Streams for a functional style, which is concise but may be less clear for beginners.

Complexity: O(n) time, O(n) space

Time Complexity

The function calls itself n times, so the time grows linearly with the input number.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used is proportional to n.

Which Approach is Fastest?

The iterative approach uses O(1) space and is generally faster due to no overhead of recursive calls, but recursion is more elegant and easier to understand.

ApproachTimeSpaceBest For
RecursiveO(n)O(n)Clear logic, small inputs
IterativeO(n)O(1)Large inputs, performance
StreamsO(n)O(1)Functional style, concise code
💡
Always include a base case in recursion to prevent infinite calls.
⚠️
Forgetting the base case causes the recursion to never stop and leads to a stack overflow error.