Here we will write a simple program to print Fibonacci Series. In the Fibonacci Series, the next number is the sum of the previous two numbers.

C# Program to Print Fibonacci Series

In the Fibonacci Series, the next number is the sum of the previous two numbers.

 0, 1, 1, 2, 3, 5, 8, 13, 21 etc

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


class Program
{
static void Main(string[] args)
{
int iFirst = 0, iSecond = 1, iThird, iCount, iInput;
Console.Write("Enter the number of elements: ");
iInput = int.Parse(Console.ReadLine());
Console.Write(iFirst + " " + iSecond + " "); //printing 0 and 1 
for (iCount = 2; iCount < iInput; ++iCount) //loop starts from 2 because 0 and 1 are already printed 
{
iThird = iFirst + iSecond;
Console.Write(iThird + " ");
iFirst = iSecond;
iSecond = iThird;
}
Console.ReadKey();
}
}

Output:

Enter the number of elements: 6
>0 1 1 2 3 5

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are welcome.

Leave a Comment