Write a C# program to find the Fibonacci Series

How to write a C# program to find the Fibonacci Series. Fibonacci series in C#.

Before dive into code let’s understand what is Fibonacci series and then will move to the logic to solve the Fibonacci problem in C#.

What is the Fibonacci Series?

Fibonacci series is a sum of two subsequent numbers. The first two numbers in a Fibonacci series are 0, 1 and all other subsequent numbers are the sum of its previous two numbers. Example – 0, 1, 1, 2, 3, 5

Find the Fibonacci series in C#

 
using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 0, num2 = 1, num3, i, number;
            Console.Write("Enter the number of elements you want to print: ");
            number = int.Parse(Console.ReadLine());
            //prin 0 and 1 manually
            Console.Write(num1 + " " + num2 + " ");
            //Start loop from 2 and execute till the input number-1.
            for (i = 2; i < number; ++i)    
            {
                num3 = num1 + num2;
                Console.Write(num3 + " ");
                num1 = num2;
                num2 = num3;
            }
            
            Console.ReadLine();
        }
    }
}
 

Now understand the code line by line. how the code will compile and generate the Fibonacci series as output.

Add a Comment

Your email address will not be published. Required fields are marked *

10 + thirteen =