What if a simple function could solve a complex sequence by calling itself again and again?
Why Fibonacci Using Recursion in DSA C?
Imagine you want to find the 10th number in the Fibonacci sequence by writing down each number one by one on paper.
You start adding the last two numbers repeatedly to get the next one.
Doing this by hand is slow and easy to make mistakes.
As the number gets bigger, you have to add many pairs again and again, which wastes time and causes confusion.
Using recursion, the computer can break the problem into smaller parts automatically.
It calls the same function to find smaller Fibonacci numbers and combines them to get the answer.
This saves you from repeating the same work manually.
int fibonacci(int n) {
int a = 0, b = 1, c, i;
if (n == 0) return a;
if (n == 1) return b;
for (i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}Recursion lets you solve complex problems by breaking them into simpler, repeatable steps automatically.
Calculating the number of ways to climb stairs when you can take one or two steps at a time uses the Fibonacci pattern.
Manual addition for Fibonacci is slow and error-prone.
Recursion breaks the problem into smaller calls, simplifying the process.
This method helps solve many problems that build on smaller subproblems.
Practice
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.Step 2: Relate to recursion
Recursion calls the function for the two previous numbers and adds their results to get the current number.Final Answer:
Each number is the sum of the two preceding numbers -> Option AQuick Check:
Fibonacci = sum of two previous numbers [OK]
- Thinking Fibonacci multiplies previous numbers
- Confusing subtraction or division with addition
- Assuming Fibonacci uses only one previous number
Solution
Step 1: Identify base cases for Fibonacci
Fibonacci sequence starts with 0 and 1 for n=0 and n=1 respectively, so base case returns n when n is 0 or 1.Step 2: Match base case syntax
The condition if (n <= 1) return n; correctly returns 0 for n=0 and 1 for n=1.Final Answer:
if (n <= 1) return n; -> Option CQuick Check:
Base case returns n for n=0 or 1 [OK]
- Returning 2 for base case
- Returning 0 for n=1 incorrectly
- Missing base case leading to infinite recursion
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
int main() {
printf("%d", fib(4));
return 0;
}Solution
Step 1: Calculate fib(4) using recursion
fib(4) = fib(3) + fib(2) - fib(3) = fib(2) + fib(1) = (fib(1)+fib(0)) + 1 = (1+0)+1=2 - fib(2) = fib(1) + fib(0) = 1 + 0 = 1 So fib(4) = 2 + 1 = 3Step 2: Confirm output printed
The printf prints the integer 3.Final Answer:
3 -> Option BQuick Check:
fib(4) = 3 [OK]
- Confusing fib(4) with fib(5)
- Adding wrong fib values
- Forgetting base cases
int fib(int n) {
if (n == 0) return 0;
else return fib(n-1) + fib(n-2);
}Solution
Step 1: Check base cases
Function only returns 0 for n==0 but misses base case for n==1, causing infinite recursion for n=1.Step 2: Fix by adding base case for n==1
Adding if (n == 1) return 1; fixes recursion and returns correct Fibonacci values.Final Answer:
Add base case for n == 1 to return 1 -> Option AQuick Check:
Both n=0 and n=1 base cases needed [OK]
- Missing base case for n=1
- Changing base case values incorrectly
- Removing else causing syntax confusion
Solution
Step 1: Understand repeated calculations in recursion
Recursive Fibonacci calls fib(n-1) and fib(n-2) repeatedly, causing many duplicate calls and inefficiency.Step 2: Use memoization to store results
Memoization saves computed Fibonacci values in a cache, so repeated calls return stored results instantly, improving speed.Final Answer:
Use memoization to store and reuse computed Fibonacci values -> Option DQuick Check:
Memoization avoids repeated work [OK]
- Increasing recursion depth doesn't fix inefficiency
- Using loops without caching still repeats work
- Calling fib(n-1) twice wastes time
Ready to prove it?
Pass the test and this pattern moves unknown
