📖 Scenario: You are helping a friend understand how to calculate Fibonacci numbers using a simple method that calls itself. This method is called recursion. Fibonacci numbers are a sequence where each number is the sum of the two before it, starting with 0 and 1.
🎯 Goal: Build a small program that uses a recursive function to find the Fibonacci number at a given position.
📋 What You'll Learn
Create a variable to hold the position number in the Fibonacci sequence.
Create a recursive function called Fibonacci that takes an integer parameter n.
The function should return the Fibonacci number at position n.
Print the Fibonacci number for the given position.
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve problems that break down into smaller similar problems.
💼 Career
Recursion is a key concept in programming interviews and software development for tasks like tree traversal, sorting, and dynamic programming.
Progress0 / 4 steps
1
Create the Fibonacci position variable
Create an integer variable called position and set it to 7.
DSA C#
Hint
Use int position = 7; to create the variable.
2
Define the recursive Fibonacci function
Define a recursive function called Fibonacci that takes an integer parameter n and returns an integer. The function should return n if n is 0 or 1. Otherwise, it should return the sum of Fibonacci(n - 1) and Fibonacci(n - 2).
DSA C#
Hint
Use if (n == 0 || n == 1) to check the base cases and call Fibonacci recursively for other cases.
3
Call the Fibonacci function
Create an integer variable called result and set it to the value returned by calling Fibonacci(position).
DSA C#
Hint
Use int result = Fibonacci(position); to call the function and save the answer.
4
Print the Fibonacci result
Print the value of the result variable using Console.WriteLine.
DSA C#
Hint
Use Console.WriteLine(result); to show the Fibonacci number.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the product of the two preceding numbers
B. Each number is the sum 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: Apply to recursion context
Recursion calculates this by calling the function for the two previous numbers and adding their results.
Final Answer:
Each number is the sum of the two preceding numbers -> Option B
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Remember Fibonacci adds two previous numbers [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 C# syntax to define a recursive Fibonacci function header?
easy
A. void Fibonacci(int n)
B. int Fibonacci()
C. int Fibonacci(int n)
D. int Fibonacci(int)
Solution
Step 1: Identify function return type and parameters
The Fibonacci function returns an integer and takes an integer parameter for the position.
Step 2: Match correct C# syntax
Correct syntax includes return type, function name, and parameter with type: int Fibonacci(int n)
Final Answer:
int Fibonacci(int n) -> Option C
Quick Check:
Function returns int and takes int parameter [OK]
Hint: Function must return int and take int parameter [OK]