Write a C# program that generates the Fibonacci series.
The Fibonacci series starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. For example, it begins with 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.
Mathematical Formula:
F(n) = F(n-1) + F(n-2)
where:
- F(n) is the nth term of the Fibonacci series
- F(n-1) is the (n-1)th term and F(n-2) is the (n-2)th term.
The Fibonacci series starts with F(0) = 0 and F(1) = 1, and each subsequent term is the sum of the two preceding terms.
- Fibonacci Series Program in C# (For Loop)
- Fibonacci Series in C# using Recusrion
- Print the nth Fibonacci number from the Fibonacci series
In this method, we use a for loop to generate n fibonacci series.
Here is source code of the C# program which generates a Fibonacci series.The C# program is successfully compiled and executed with Microsoft Visual Studio.The program output is also shown below.
/* * C# Program to Generate Fibonacci Series */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace fibonaci { class Program { static void Main(string[] args) { int i, count, f1 = 0, f2 = 1, f3 = 0; Console.Write("Enter the Limit : "); count = int.Parse(Console.ReadLine()); Console.WriteLine(f1); Console.WriteLine(f2); for (i = 0; i <= count; i++) { f3 = f1 + f2; Console.WriteLine(f3); f1 = f2; f2 = f3; } Console.ReadLine(); } } }
1. In this C# program, we are reading the limit to generate the Fibonacci series using ‘count’ variable.
2. The numbers that precede the series are 0 and 1. The next number is found by adding up the two numbers before it. Using for loop generate Fibonacci series.
3. Initialize the value of ‘i’ variable as 0 and check the condition that the value of ‘i’ variable is less than or equal to the value of ‘count’ variable.
4. Calculate the sum of ‘f1’ and ‘f2’, then print the Fibonacci series value.
In this case, entering “10” as the limit generate the fibonacci series.
Enter the Limit : 10 0 1 1 2 3 5 8 13 21 34 55 89 144
In this method, we use a recusrion to generate n fibonacci series.
Here is source code of the C# program which generates a Fibonacci series using recursion. The C# program is successfully compiled and executed with Microsoft Visual Studio.The program output is also shown below.
/* * C# Program to Generate Fibonacci Series using Recursion */ using System; namespace Fibonacci { class Program { static void Main() { Console.Write("Enter the limit to generate the Fibonacci Series: "); int count = int.Parse(Console.ReadLine()); Console.WriteLine("Fibonacci Series:"); for (int i = 0; i < count; i++) { Console.Write(Fibonacci(i) + " "); } Console.ReadLine(); } static int Fibonacci(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n - 1) + Fibonacci(n - 2); } } }
1. The program prompts the user to input the number of terms in the Fibonacci series.
2. It uses a recursive method Fibonacci(n) to calculate the nth term of the series.
3. The method handles base cases for n=0 and n=1, returning 0 and 1 respectively.
4. For other values of n, it calls itself with n-1 and n-2 as arguments until it reaches the base cases.
5. The generated Fibonacci series is displayed to the user.
In this case, entering “7” as the limit generate the fibonacci series.
Enter the limit to generate the Fibonacci Series: 7 Fibonacci Series: 0 1 1 2 3 5 8
In this method, we print the nth Fibonacci number from the Fibonacci series without utilizing recursion.
Here is source code of the C# program to print the nth Fibonacci number from the Fibonacci series. The C# program is successfully compiled and executed with Microsoft Visual Studio.The program output is also shown below.
/* * Find the nth Fibonacci number from the Fibonacci series */ using System; namespace Fibonacci { class Program { static void Main() { Console.Write("Enter the value of 'n' to get the nth Fibonacci number: "); int n = int.Parse(Console.ReadLine()); int result = Fibonacci(n); Console.WriteLine($"The {n}th Fibonacci number is: {result}"); Console.ReadLine(); } static int Fibonacci(int n) { if (n == 0 || n == 1) return n; int f1 = 0, f2 = 1, f3 = 0; for (int i = 2; i <= n; i++) { f3 = f1 + f2; f1 = f2; f2 = f3; } return f3; } } }
1. The user is asked to input the value of ‘n‘, representing the position of the desired Fibonacci number in the series.
2. The program calculates the nth Fibonacci number using a loop, without recursion.
3. Base cases for n=0 and n=1 return n directly.
4. For other values of n, a for loop is used to calculate the Fibonacci number iteratively.
5. The calculated Fibonacci number is displayed to the user as the output.
In this case, enter “6” as the value of ‘n’ to get the nth Fibonacci number from the series.
Enter the value of 'n' to get the nth Fibonacci number: 6 The 6th Fibonacci number is: 8
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Practice MCA MCQs
- Buy MCA Books
- Buy C# Books
- Apply for Computer Science Internship