Write a Program in c# to get Fibonacci Series.

C# Program to generate Fibonacci Series.

 C#   
 

Posted: January 1, 2019. | By: mustafa

Fibonacci Series starts with 0 & 1 and the next preceding number is sum of two last numbers in the series.
To generate the Fibonacci Series in computer program using c# is given below.

C# program to generate Fibonacci Series

static void Main(string[] args)
 {
 Console.WriteLine("Press y/n to call Fibonacci Series function.");
 var value = Console.Read();
 if (value == 'y')
 GetFibonacciSeries();
 }
 
 

public static void GetFibonacciSeries()
 {
 int r = 0, n = 1, i = 0, m = 0, t = 0;
 
 Console.Write("Enter the number of elements: ");
 m = int.Parse(Console.ReadLine());

Console.Write(r + "\n" + n + "\n"); //printing 0 and 1

for (i = 1; i <= m; i++)
 {
 t = n + r;
 Console.WriteLine(t);
 
 r = n;
 n = t;
 }

Console.WriteLine("Press any key to continue...");
Console.ReadKey();
 }
 


Output:

Press y/n to call Fibonacci Series function.
y
Enter the number of elements: 10
0
1
1
2
3
5
8
13
21
34
55
89

Press any key to continue…


related posts

Back to top