Latest Post

Tuesday, 3 January 2017

How to write program to Print the Fibonacci series in C#

Write a program in C# to print the Fibonacci series.

Following is the simple C# code :

class Lab
    {
        static void Main(string[] args)
        {
            int i, count, Fib1 = 0, Fib2 = 1, Fib3 = 0;
            Console.Write("Enter the Fib Limit : ");
            count = int.Parse(Console.ReadLine());
            Console.WriteLine("Fibonacci sequence are ");
            Console.WriteLine(Fib1);
            Console.WriteLine(Fib2);
            for (i = 0; i <= count; i++)
            {
                Fib3 = Fib1 + Fib2;
                Console.WriteLine(Fib3);
                Fib1 = Fib2;
                Fib2 = Fib3;
            }
            Console.ReadLine();

        }
    }

Output:-

Enter the Fib Limit : 5

Fibonacci sequence are
0
1
1
2
3
5
8
13


Government Jobs