Factorial in Java
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.
Java Program
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
Core Logic
Recursion fits factorial naturally — a base case stops the descent, and each call multiplies its input by whatever the smaller call returns.
- 1The base case
if (n == 0) return 1;stops the recursion from descending forever. - 2Every other call returns
n * factorial(n - 1), deferring its own result until the smaller call finishes. - 3Each call pushes a new frame onto the call stack, so the calls descend:
factorial(6) → factorial(5) → ... → factorial(0). - 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
Approach 2: Iterative Loop
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
Core Logic
A plain loop with a running product gets the same answer without any recursive call-stack overhead.
- 1
resultstarts at1, the correct value for0!and1!. - 2The loop runs
ifrom2up ton, since multiplying by 1 wouldn't change anything. - 3Each pass does
result *= i, accumulating the running product. - 4Once the loop ends,
resultholds the full factorial — no unwinding step needed.
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
Approach 3: Stream Reduce
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
Core Logic
Or skip both the loop and the recursion — a single Stream reduction multiplies the whole range together in one expression.
- 1
LongStream.rangeClosed(2, n)produces a stream of the numbers2, 3, 4, 5, 6. - 2
.reduce(1, (a, b) -> a * b)combines every element into a single value, starting from the identity1and multiplying pairwise. - 3The lambda
(a, b) -> a * bis applied repeatedly: first to1and2, then to that result and3, and so on. - 4The final accumulated value is the factorial.
[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.