Fibonacci Series in C#

C# program to generate Fibonacci Series.

Source code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Fibonacci
{
    class Code
    {
        static void Main(string[] args)
        {
            int i, count, fib1 = 0, fib2 = 1, fib3 = 0;
            Console.Write("Enter the number of values:
 ");
            count = int.Parse(Console.ReadLine());
            Console.WriteLine("The series:");
            Console.WriteLine(fib1);
            Console.WriteLine(fib2);
            for (i = 0; i < count-2; i++)
            {
                fib3 = fib1 + fib2;
                fib1 = fib2;
                fib2 = fib3;
            Console.WriteLine(fib3);
            }
            Console.ReadLine();
 
        }
    }
}

Output :

Enter the number of values:
10
The series:
0
1
1
2
3
5
8
13
21
34

Algorithm :

  1. Get the range of limit from the user.
  2. Initialize 0 and 1 as first two numbers.
  3. Add the successive numbers and print as per the given range.

Output :


                

Comments :