Write a C# program to generate the Fibonacci sequence up to a given number n.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones.
Write a C# program to generate the Fibonacci sequence up to a given number n.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones.
Student
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Fibonacci Sequence in C#
Below is an example of how you can generate the Fibonacci sequence using an iterative approach in C#
Example-
In the above example-
We determine how many elements in Fibonacci numbers to print (
maxNumber).We define the function
Fibonaccito calculate the nth Fibonacci number.In the
Fibonaccifunction, we start with the first two Fibonacci numbers,aandb.Then we iterate from the third Fibonacci number to the nth Fibonacci number, counting each Fibonacci number several times.
Finally, we iterate the loop in
Mainmethod for themaxNumbertimes to generate the Fibonacci numbers as you need.Output-
Also, Read: Pascal Triangle