Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the nth value of a Fibonacci series using recursion in C#?
The Fibonacci series is a sequence where each number is the sum of the two preceding numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21... In C#, we can use recursion to calculate the nth Fibonacci number by breaking the problem into smaller subproblems.
Recursion works by having the method call itself with reduced parameters until it reaches a base case. For Fibonacci, the base cases are F(0) = 0 and F(1) = 1.
Syntax
Following is the syntax for a recursive Fibonacci method −
public int Fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
How Fibonacci Recursion Works
The recursive approach breaks down F(n) into F(n-1) + F(n-2), continuing until it reaches the base cases. Here's how the recursion tree looks for F(5) −
Using Recursive Fibonacci Method
Example
using System;
public class FibonacciDemo {
public static void Main(string[] args) {
FibonacciDemo demo = new FibonacciDemo();
int position = 7;
int result = demo.GetFibonacci(position);
Console.WriteLine("{0}th Fibonacci number = {1}", position, result);
Console.WriteLine("\nFirst 10 Fibonacci numbers:");
for (int i = 0; i < 10; i++) {
Console.Write(demo.GetFibonacci(i) + " ");
}
}
public int GetFibonacci(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
} else {
return GetFibonacci(n - 1) + GetFibonacci(n - 2);
}
}
}
The output of the above code is −
7th Fibonacci number = 13 First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Optimized Recursive Approach with Memoization
The basic recursive approach has exponential time complexity due to repeated calculations. We can optimize it using memoization −
Example
using System;
using System.Collections.Generic;
public class OptimizedFibonacci {
private static Dictionary<int, int> memo = new Dictionary<int, int>();
public static void Main(string[] args) {
int position = 40;
int result = GetFibonacciMemo(position);
Console.WriteLine("{0}th Fibonacci number = {1}", position, result);
}
public static int GetFibonacciMemo(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
if (memo.ContainsKey(n)) {
return memo[n];
}
memo[n] = GetFibonacciMemo(n - 1) + GetFibonacciMemo(n - 2);
return memo[n];
}
}
The output of the above code is −
40th Fibonacci number = 102334155
Performance Comparison
| Approach | Time Complexity | Space Complexity | Best for |
|---|---|---|---|
| Basic Recursion | O(2^n) | O(n) | Small values of n (< 20) |
| Memoized Recursion | O(n) | O(n) | Larger values, reusable results |
Conclusion
Recursive Fibonacci calculation in C# demonstrates the power of recursion by breaking complex problems into simpler subproblems. While basic recursion is elegant, memoization significantly improves performance for larger values by avoiding redundant calculations.
