📖 Scenario: Imagine you are helping a chef organize recipes. Some recipes are simple and repeat steps many times, while others have steps that call themselves again and again. You want to understand when to use repeating loops (iteration) and when to use recipes that call themselves (recursion).
🎯 Goal: You will create two ways to calculate the factorial of a number: one using recursion and one using iteration. Then, you will compare their outputs to see they give the same result.
📋 What You'll Learn
Create a variable number with the value 5
Write a recursive function factorial_recursive that calculates factorial
Write an iterative function factorial_iterative that calculates factorial
Print the results of both functions for number
💡 Why This Matters
🌍 Real World
Understanding when to use recursion or iteration helps in writing efficient code for tasks like searching, sorting, and processing data structures.
💼 Career
Many programming jobs require choosing the right approach for algorithms to optimize speed and memory, especially in interviews and system design.
Progress0 / 4 steps
1
Create the input number
Create an integer variable called number and set it to 5.
DSA C++
Hint
Use int number = 5; to create the variable.
2
Write the recursive factorial function
Write a recursive function called factorial_recursive that takes an integer n and returns the factorial of n. Use the base case if (n == 0) return 1; and the recursive call return n * factorial_recursive(n - 1);.
DSA C++
Hint
Remember the factorial of 0 is 1. For other numbers, multiply n by factorial of n-1.
3
Write the iterative factorial function
Write an iterative function called factorial_iterative that takes an integer n and returns the factorial of n. Use a for loop from 1 to n multiplying a result variable.
DSA C++
Hint
Start with result = 1. Multiply it by each number from 1 to n in a loop.
4
Print the factorial results
Print the factorial of number using both factorial_recursive and factorial_iterative functions. Use std::cout to print the results on separate lines.
DSA C++
Hint
Use std::cout << factorial_recursive(number) << std::endl; and similarly for factorial_iterative.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When the problem can be divided into similar smaller subproblems
B. When you need to process a large list without stack overflow risk
C. When you want to avoid function call overhead
D. When the problem requires simple repeated loops
Solution
Step 1: Understand recursion use case
Recursion works well when a problem can be broken down into smaller versions of itself, like tree traversals or factorial calculation.
Step 2: Compare with iteration scenarios
Iteration is better for simple repeated tasks without nested subproblems, so recursion is preferred for divide-and-conquer problems.
Final Answer:
When the problem can be divided into similar smaller subproblems -> Option A
Quick Check:
Recursion fits divide-and-conquer problems [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always better
Ignoring stack overflow risk in recursion
Confusing recursion with simple loops
2. Which of the following is the correct syntax to write a recursive function in C++?
easy
A. void func() { if(condition) func(); }
B. void func() { func(); }
C. void func() { while(true) func(); }
D. void func() { for(int i=0; i<10; i++) func(); }
Solution
Step 1: Identify base case in recursion
Recursive functions must have a condition to stop calling themselves, usually an if statement.
Step 2: Check options for base case usage
void func() { if(condition) func(); } uses if(condition) to control recursion, preventing infinite calls. Others cause infinite recursion or loops.
Final Answer:
void func() { if(condition) func(); } -> Option A
Quick Check:
Recursion needs a stopping condition [OK]
Hint: Recursive functions must have a stopping condition [OK]
Common Mistakes:
Writing recursion without base case
Using loops inside recursion incorrectly
Confusing infinite loops with recursion
3. What is the output of the following C++ code?
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(4);
return 0;
}