Home Programs How to display a Fibonacci Series in C#

How to display a Fibonacci Series in C#

In this article we will show you how show a fibonacci series

In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones.

The first two numbers of fibonacci series are 0 and 1., although some authors omit the initial terms and start the sequence from 1 and 1 or from 1 and 2. Starting from 0 and 1, the next few values in the sequence are:[1]

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Example

 

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = 0, n2 = 1, n3, i, myNumber;
            Console.Write("Enter the number of elements: ");
            myNumber = int.Parse(Console.ReadLine());
            Console.Write(n1 + " " + n2 + " "); //print 0 and 1  
            
            for (i = 2; i < myNumber; ++i) //loop starts from 2   
            {
                n3 = n1 + n2;
                Console.Write(n3 + " ");
                n1 = n2;
                n2 = n3;
            }

            Console.ReadLine();
        }
    }
}

 

Here is a test run

Enter the number of elements: 15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

You may also like