Fibonacci Series in C#

In the case of Fibonacci series, the next number is the sum of the previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of Fibonacci series are 0 and 1.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            int val1 = 0, val2 = 1, val3, i, number;
            Console.WriteLine("Please Enter any number");
            number = int.Parse(Console.ReadLine());
            Console.WriteLine(val1 + " " + val2);
            for (i=2;i<=number;i++)
            {
                val3 = val1 + val2;
                Console.WriteLine(val3);
                val1 = val2;
                val2 = val3;
            }


            Console.ReadLine();
        }
    }

}

Comments

Popular posts from this blog

Prime Number Program in C#

Joins in SQL server