Fibonacci Series Program in C#

Problem Description

Write a C# program that generates the Fibonacci series.

What is 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.

Different ways to implement the Fibonacci in C#:

advertisement
advertisement
Method 1: Fibonacci Series Program in C# using For Loop

In this method, we use a for loop to generate n fibonacci series.

Program/Source Code

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();
 
        }
    }
}
Program Explanation

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.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases

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

Method 2: using Recusrion

In this method, we use a recusrion to generate n fibonacci series.

Program/Source Code

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.

advertisement
/*
 * 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);
        }
    }
}
Program Explanation

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.

Runtime Test Cases

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

advertisement
Method 3: Print the nth Fibonacci number from the Fibonacci series

In this method, we print the nth Fibonacci number from the Fibonacci series without utilizing recursion.

Program/Source Code

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;
        }
    }
}
Program Explanation

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.

Program 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.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.