📖 Scenario: You are helping a friend understand how to calculate Fibonacci numbers using a simple function that calls itself.The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two before it.
🎯 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.
Call the fibonacci function from main to find the Fibonacci number for n = 6.
Print the result using printf.
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve many problems.
💼 Career
Knowing recursion and how to implement it in C is important for software development, especially in algorithm design and problem solving.
Progress0 / 4 steps
1
Create the main function and variable
Write a main function and create an integer variable called n set to 6.
DSA C
Hint
The main function is where the program starts. Declare int n = 6; inside it.
2
Write the recursive fibonacci function
Write a recursive function called fibonacci that takes an integer n and returns an integer. Use these rules: if n == 0, return 0; if n == 1, return 1; otherwise return fibonacci(n - 1) + fibonacci(n - 2).
DSA C
Hint
The function calls itself with smaller values until it reaches 0 or 1.
3
Call fibonacci function and store result
Inside main, create an integer variable called result and set it to the value returned by calling fibonacci(n).
DSA C
Hint
Call fibonacci(n) and save it in result.
4
Print the Fibonacci result
Add #include <stdio.h> at the top. Inside main, use printf to print the text "Fibonacci number at position 6 is: %d\n" with the value of result.
DSA C
Hint
Use printf with the format string and result to show the answer.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the sum of the two preceding numbers
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the division of the two preceding numbers
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 A
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds previous two numbers recursively [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing subtraction or division with addition
Assuming Fibonacci uses only one previous number
2. Which of the following is the correct base case for a recursive Fibonacci function in C?
easy
A. if (n < 2) return 2;
B. if (n == 0) return 0; else return 1;
C. if (n <= 1) return n;
D. if (n == 1) return 0;
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 C
Quick Check:
Base case returns n for n=0 or 1 [OK]
Hint: Base case returns n when n is 0 or 1 [OK]
Common Mistakes:
Returning 2 for base case
Returning 0 for n=1 incorrectly
Missing base case leading to infinite recursion
3. What is the output of the following C code snippet?
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
int main() {
printf("%d", fib(4));
return 0;
}