C# program to display fibonacci series

Fibonacci series :-The Fibonacci sequence is a series of numbers starting with 0 and 1  where a number is the addition of the last two numbers, for example 0, 1, 1, 2, 3, 5, 8, 13 etc.


Algorithm:-

(1)Start

(2)take four variable n1,n2,n3,num with n1 and n2 initial value zero and one

(3)take number from user for print fibonacci series and store in num

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleProgrammPractice

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                int n1 = 0, n2 = 1, n3, num;

               Console.Write("Enter the number of elements to print Fibonacci series: ");

                num = int.Parse(Console.ReadLine());

                Console.Write(n1 + " " + n2 + " ");  

                for (int i = 2; i < number; i++)   

                {

                    n3 = n1 + n2;

                    Console.Write(n3 + " ");

                    n1 = n2;

                    n2 = n3;

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("input is not in correct formate");

            }

            Console.ReadKey();

        }

    }

}


Output:-

Enter the number of elements to print Fibonacci series: 10

0 1 1 2 3 5 8 13 21 34

एक टिप्पणी भेजें

0 टिप्पणियाँ