📖 Scenario: You are helping a friend understand how to calculate Fibonacci numbers using a simple recursive function. Fibonacci numbers are a sequence where each number is the sum of the two before it, starting with 0 and 1.
🎯 Goal: Build a C++ program that uses a recursive function to find the Fibonacci number at a given position.
📋 What You'll Learn
Create a recursive function called fibonacci that takes an integer n and returns the Fibonacci number at position n.
Use base cases for n == 0 and n == 1 to return 0 and 1 respectively.
Call the fibonacci function with n = 6 and store the result in a variable called result.
Print the value of result.
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve problems that can be broken down into smaller similar problems.
💼 Career
Recursion is a fundamental programming concept used in many technical interviews and real-world software development tasks involving trees, graphs, and divide-and-conquer algorithms.
Progress0 / 4 steps
1
Create the Fibonacci function signature
Write the function signature for a recursive function called fibonacci that takes an integer parameter n and returns an integer.
DSA C++
Hint
Start by writing the function header only, without the body.
2
Add base cases for recursion
Inside the fibonacci function, add base cases to return 0 when n == 0 and return 1 when n == 1.
DSA C++
Hint
Use two if statements to check for n == 0 and n == 1 and return the correct values.
3
Add recursive call to calculate Fibonacci
Complete the fibonacci function by returning the sum of fibonacci(n - 1) and fibonacci(n - 2) for other values of n.
DSA C++
Hint
Use the function name fibonacci recursively with n - 1 and n - 2 and add their results.
4
Call the function and print the result
In the main function, call fibonacci with 6, store the result in a variable called result, and print result.
DSA C++
Hint
Use int result = fibonacci(6); and then std::cout << result << std::endl; to print.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the square of the previous number
B. Each number is the sum of the two preceding numbers starting from 0 and 1
C. Each number is the difference between the two preceding numbers
D. Each number is the product of the two preceding numbers
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two 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 starting from 0 and 1 -> Option B
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds last two numbers recursively [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing subtraction or squares with Fibonacci
Ignoring the base cases 0 and 1
2. Which of the following is the correct base case for a recursive Fibonacci function in C++?
easy
A. if (n == 1) return 0; if (n == 2) return 1;
B. if (n <= 2) return n;
C. if (n == 0) return 0; if (n == 1) return 1;
D. if (n == 0) return 1; if (n == 1) return 1;
Solution
Step 1: Identify correct Fibonacci base cases
Fibonacci sequence starts with 0 at position 0 and 1 at position 1, so base cases must return these values.
Step 2: Match base cases to code
if (n == 0) return 0; if (n == 1) return 1; correctly returns 0 for n=0 and 1 for n=1, matching Fibonacci definition.
Final Answer:
if (n == 0) return 0; if (n == 1) return 1; -> Option C
Quick Check:
Base cases match Fibonacci start [OK]
Hint: Base cases: 0 returns 0, 1 returns 1 [OK]
Common Mistakes:
Using wrong base values like 1 for n=0
Returning n for n <= 2 which is incorrect
Confusing base cases with general recursive step
3. What is the output of the following C++ code?
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
int main() {
std::cout << fib(5);
return 0;
}
medium
A. 5
B. 8
C. 3
D. 13
Solution
Step 1: Calculate fib(5) using recursion
fib(5) = fib(4) + fib(3). Calculate fib(4) = fib(3) + fib(2), fib(3) = fib(2) + fib(1), and so on until base cases.