Recursion· 15 min

Factorial (Recursion)

n! = n × (n-1)! — base case and recursive case

Setup

Total calls: 1
Step 1 / 20

Current step

1 / 20

Call factorial(5).

Step log

Push (call)Call StackPop (return)
Stack is empty
Stack grows down — each frame is a pending multiplication
factorial.java
1int factorial(int n) {
2 if (n <= 1) return 1;
3 return n * factorial(n - 1);
4}
● Java

Factorial

Computes n! = n × (n-1) × ... × 1. Each call multiplies n by factorial(n-1) until n=1.

Time
O(n)
Space
O(n)
Calls so far
1
Stack depth
0