# Fibonacci Array Program Help

• ### Question

• Hello,

I am a beginner to arrays in programming.

I would like to write a program in visual studio to display up to the Nth elements in a Fibonacci Series. When you run the program, I will let the user specify the value of N in the console, and the output will display the series up to Nth term. But I would like to use an array. How would I do this.

Thanks.

Thursday, April 2, 2020 5:15 PM

• The first observation is that you don´t really need to use an array. You can simply write a loop and print the numbers as they are being generated. You only need the two last numbers to calculate the next one, so you can simply keep them in two independent variables. Of course, nothing stops you from putting the two numbers into an array of two elements if you so desire.

So, start by dimensioning an array of two elements, and initialize the two elements to 1. These will be the two first Fibonacci numbers.

var lastTwo = new int[]{1,1};

Print these two ones to the console.

Then, write a loop that does N-2 iterations. On each iteration, add the two numbers and print the result. Then move the second item in the array into the first and move the sum you calculated into the second.

That's it. This will print out the Fibonacci numbers. I'm not writing the code for you because this looks like an academic exercise, so you should do the work yourself.

Thursday, April 2, 2020 9:21 PM
• Hint : Fibonacci + C# on Google...
Thursday, April 2, 2020 9:29 PM

### All replies

• The first observation is that you don´t really need to use an array. You can simply write a loop and print the numbers as they are being generated. You only need the two last numbers to calculate the next one, so you can simply keep them in two independent variables. Of course, nothing stops you from putting the two numbers into an array of two elements if you so desire.

So, start by dimensioning an array of two elements, and initialize the two elements to 1. These will be the two first Fibonacci numbers.

var lastTwo = new int[]{1,1};

Print these two ones to the console.

Then, write a loop that does N-2 iterations. On each iteration, add the two numbers and print the result. Then move the second item in the array into the first and move the sum you calculated into the second.

That's it. This will print out the Fibonacci numbers. I'm not writing the code for you because this looks like an academic exercise, so you should do the work yourself.

Thursday, April 2, 2020 9:21 PM
• Hint : Fibonacci + C# on Google...
Thursday, April 2, 2020 9:29 PM