Java Tutorial
🔍
Java ProgramsRecursionFactorial

Factorial in Java

beginner·  Recursion  ·  Recursion

Problem

The factorial of a number n, written n!, is the product of every positive integer from 1 up to n.

Given a number, compute its factorial.

Input
6
Output
6! = 720

Java Program

Java
public class Factorial { static long factorial(int n) { if (n == 0) return 1; // base case return n * factorial(n - 1); // deferred until the smaller call returns } public static void main(String[] args) { int n = 6; System.out.println(n + "! = " + factorial(n)); } }

Output

6! = 720

Core Logic

Recursion fits factorial naturally — a base case stops the descent, and each call multiplies its input by whatever the smaller call returns.

How It Works
  1. 1The base case if (n == 0) return 1; stops the recursion from descending forever.
  2. 2Every other call returns n * factorial(n - 1), deferring its own result until the smaller call finishes.
  3. 3Each call pushes a new frame onto the call stack, so the calls descend: factorial(6) → factorial(5) → ... → factorial(0).
  4. 4Once the base case returns 1, the pending multiplications unwind back up the stack in reverse order.
factorial(6) unwinds as 1×1=1, 2×1=2, 3×2=6, 4×6=24, 5×24=120, 6×120=720, so the program prints 6! = 720.
💡

Key Point: Every recursive call needs a reachable base case — without n == 0 stopping the descent, this would recurse until the stack overflows.

Key Concepts

recursionbase case

Approach 2: Iterative Loop

Java
public class FactorialIterative { static long factorial(int n) { long result = 1; // Multiply every number from 2 up to n into the running product for (int i = 2; i <= n; i++) { result *= i; } return result; } public static void main(String[] args) { int n = 6; System.out.println(n + "! = " + factorial(n)); } }

Output

6! = 720

Core Logic

A plain loop with a running product gets the same answer without any recursive call-stack overhead.

How It Works
  1. 1result starts at 1, the correct value for 0! and 1!.
  2. 2The loop runs i from 2 up to n, since multiplying by 1 wouldn't change anything.
  3. 3Each pass does result *= i, accumulating the running product.
  4. 4Once the loop ends, result holds the full factorial — no unwinding step needed.
For n = 6, result updates through 1, 2, 6, 24, 120, 720 as i goes from 2 to 6.
💡

Key Point: This uses O(1) stack space versus the recursive version's O(n) — for large n, the iterative loop avoids any risk of a StackOverflowError.

Key Concepts

for looprunning productiteration

Approach 3: Stream Reduce

Java
import java.util.stream.LongStream; public class FactorialStream { public static void main(String[] args) { int n = 6; // Multiplies every number in the range together, starting from the identity 1 long result = LongStream.rangeClosed(2, n).reduce(1, (a, b) -> a * b); System.out.println(n + "! = " + result); } }

Output

6! = 720

Core Logic

Or skip both the loop and the recursion — a single Stream reduction multiplies the whole range together in one expression.

How It Works
  1. 1LongStream.rangeClosed(2, n) produces a stream of the numbers 2, 3, 4, 5, 6.
  2. 2.reduce(1, (a, b) -> a * b) combines every element into a single value, starting from the identity 1 and multiplying pairwise.
  3. 3The lambda (a, b) -> a * b is applied repeatedly: first to 1 and 2, then to that result and 3, and so on.
  4. 4The final accumulated value is the factorial.
Reducing [2, 3, 4, 5, 6] with multiplication and an identity of 1 produces 1×2×3×4×5×6 = 720.
💡

Key Point: reduce() with a multiplication lambda is a one-line way to express 'multiply everything together' — it reads close to the mathematical definition of factorial, at the cost of being slightly less familiar to read than a plain loop.

Key Concepts

StreamLongStream.rangeClosed()reduce()

Related Programs