Fibonacci in C#

It is a pattern when you add the 2 previous numbers together and to get the next number. And so on.

1. 1. 1. 2. 3. 5. 8. 13. 21. 34. 55. 89. 144. And so on.

int a = 0;
int b = 1;
int c = 0;
int n = 46; //to the N'th fibonacci No.

Console.WriteLine("Which Fibonacci Number do you want?");

n = Convert.ToInt16(Console.ReadLine());

if (n != 1)
{
for (int i = 1; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
Console.WriteLine("the {0}th Fibonacci number is {1}", n, a);
}
else
{
Console.WriteLine("the {0}st Fibonacci number is 1", n);
}
Console.ReadKey();

0 comments:

Post a Comment