Article

Fibonacci Series in C#

Fibonacci Series in C#

Fibonacci series means, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 etc. The first two numbers of Fibonacci series are 0 and 1. Like
0+1=1, 1+1=2, 1+2=3
Here you can see next number is sum of previous two number.

Code for Fibonacci Series in C#

using System;

namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int num1 = 0, num2 = 1, num3, i, number;
Console.Write(“Enter the number of elements: “);
number = int.Parse(Console.ReadLine());
//i have assign num1 and num2 for printing 0 and 1
Console.Write(num1 + ” ” + num2 + ” “);
//loop will begin from 2 because 0 and 1 are already printed
for (i = 2; i < number; ++i)
{
num3 = num1 + num2;
Console.Write(num3 + ” “);
num1 = num2;
num2 = num3;
}
Console.ReadLine();
}
}
}

Output
fibonacci-series-in-csharp

Leave a Reply

Your email address will not be published.