What if a function could solve a big problem by solving smaller versions of itself again and again?
Why Factorial Using Recursion in DSA C++?
Imagine you need to find the factorial of a number like 5 by multiplying all numbers from 5 down to 1 manually: 5 * 4 * 3 * 2 * 1.
Doing this by hand or writing many lines of code for each number is tiring and error-prone.
Manually multiplying each number requires writing repetitive code for every case, which is slow and easy to mess up.
It also becomes impossible to handle bigger numbers without making mistakes or writing huge code blocks.
Recursion lets the function call itself to handle smaller parts of the problem automatically.
For factorial, the function multiplies the current number by the factorial of the number just before it, until it reaches 1.
This makes the code short, clean, and easy to understand.
int factorial = 1; for (int i = 5; i > 0; i--) { factorial *= i; } return factorial;
int factorial(int number) {
if (number <= 1) return 1;
return number * factorial(number - 1);
}Recursion enables solving complex problems by breaking them into smaller, similar problems automatically.
Calculating combinations in probability, where factorials are needed to find how many ways to choose items from a group.
Manual multiplication is repetitive and error-prone.
Recursion simplifies the problem by calling itself with smaller inputs.
Factorial using recursion is clean, short, and easy to maintain.
Practice
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.Step 2: Compare options
Only The product of all positive integers up to that number correctly describes this product, others describe sum or unrelated operations.Final Answer:
The product of all positive integers up to that number -> Option AQuick Check:
Factorial = product of integers [OK]
- Confusing factorial with sum
- Thinking factorial divides or doubles the number
- Mixing factorial with addition
Solution
Step 1: Identify factorial base case
By definition, 0! = 1, so the base case must return 1 when n is 0.Step 2: Check options
Only if (n == 0) return 1; returns 1 when n is 0, others return incorrect values.Final Answer:
if (n == 0) return 1; -> Option DQuick Check:
Base case 0! = 1 [OK]
- Returning 0 for base case
- Using n == 1 but returning wrong value
- Missing base case causing infinite recursion
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(4);
return 0;
}Solution
Step 1: Trace recursive calls for factorial(4)
factorial(4) calls 4 * factorial(3), factorial(3) calls 3 * factorial(2), factorial(2) calls 2 * factorial(1), factorial(1) calls 1 * factorial(0), factorial(0) returns 1.Step 2: Calculate values bottom-up
factorial(1) = 1 * 1 = 1, factorial(2) = 2 * 1 = 2, factorial(3) = 3 * 2 = 6, factorial(4) = 4 * 6 = 24.Final Answer:
24 -> Option BQuick Check:
4! = 24 [OK]
- Confusing factorial with sum
- Forgetting base case returns 1
- Incorrect multiplication order
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n + 1);
}Solution
Step 1: Analyze recursive call
The function calls factorial(n + 1), which increases n each time, causing infinite recursion.Step 2: Correct recursive call
To reduce n towards base case, it should call factorial(n - 1) instead.Final Answer:
The recursive call should use n - 1, not n + 1 -> Option CQuick Check:
Recursive call must decrease n [OK]
- Increasing n in recursive call causing infinite loop
- Wrong base case value
- Using addition instead of multiplication
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}What is the output of
factorial(-3) and why?Solution
Step 1: Check base case condition
The base case triggers if n <= 1, so for n = -3, it returns 1 immediately.Step 2: Understand recursion behavior
Since base case covers negative inputs, no recursion occurs and function returns 1 safely.Final Answer:
1, because base case handles n <= 1 -> Option AQuick Check:
Base case covers negatives, returns 1 [OK]
- Assuming negative input causes infinite recursion
- Expecting error or zero for negative input
- Ignoring base case condition
Ready to prove it?
Pass the test and this pattern moves unknown
