📖 Scenario: Imagine you are helping a friend understand two ways to solve problems: recursion and iteration. You will create simple C programs to see how each method works and when one might be better than the other.
🎯 Goal: Build two C functions to calculate the factorial of a number: one using recursion and one using iteration. Then compare their outputs for the same input.
📋 What You'll Learn
Create a variable called number with the value 5
Write a recursive function called factorial_recursive that takes an int n and returns the factorial of n
Write an iterative function called factorial_iterative that takes an int n and returns the factorial of n
Print the results of both functions for the variable number
💡 Why This Matters
🌍 Real World
Understanding recursion and iteration helps solve many programming problems like searching, sorting, and navigating data structures.
💼 Career
Many software jobs require knowing when to use recursion or iteration for efficient and clear code.
Progress0 / 4 steps
1
Create the input variable
Create an integer variable called number and set it to 5.
DSA C
Hint
Use int number = 5; inside the main function.
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) to return 1, else return n * factorial_recursive(n - 1).
DSA C
Hint
Remember the base case for factorial is when n == 0.
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 to multiply numbers from 1 to n.
DSA C
Hint
Use a for loop from 1 to n multiplying a result variable.
4
Print the factorial results
Print the factorial of number using both factorial_recursive and factorial_iterative functions. Use printf to show the results with labels.
DSA C
Hint
Use printf with the format "Recursive factorial of %d is %d\n" and call factorial_recursive(number). Do the same for factorial_iterative.
Practice
(1/5)
1. Which situation is best suited for using recursion instead of iteration?
easy
A. When you want to avoid function calls
B. When the problem can be divided into similar smaller problems
C. When memory usage must be minimal
D. When the problem requires simple repeated loops
Solution
Step 1: Understand recursion use case
Recursion works well when a problem breaks down into smaller similar problems, like factorial or tree traversal.
Step 2: Compare with iteration use cases
Iteration is better for simple repeated loops without breaking problems into smaller parts.
Final Answer:
When the problem can be divided into similar smaller problems -> Option B
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking recursion is always better for memory
Confusing iteration as better for divide-and-conquer
Ignoring function call overhead in recursion
2. Which of the following is the correct syntax for 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 recursive call with base condition
Recursive functions must have a condition to stop calling themselves to avoid infinite recursion.
Step 2: Check options for base condition
void func() { if(condition) func(); } calls itself only if a condition is true, which is correct recursive syntax.