Hello, I’m working on this task:
Fibonacci numbers are characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8… To get the next number, you must sum the two previous values; in this case, 8 + 5 = 13; thus, 13 is the next number in the sequence. The sequence always begins with 1, 1.
Write a method called “GetNextFibonacci” that takes two integers representing two sequential Fibonacci numbers and returns the next Fibonacci number. The Main method should read an int value n from the console, and then, using GetNextFibonacci, output the first n Fibonacci numbers each delimited by a single space. For the purpose of this program, assume that n will always be greater than 2 and less than or equal to 25. Here’s an example:
>10
1 1 2 3 5 8 13 21 34 55
Here is my code:
namespace Methods
{
class Fibonacci
{
static void Main(string[] args)
{
int count = int.Parse(Console.ReadLine());
int beforePrevious = 1;
Console.Write($"{beforePrevious} ");
int previous = 1;
Console.Write($"{previous} ");
for (int i = 0; i < (count - 2); i++)
{
GetNextFibonacci(beforePrevious, previous);
}
}
static int GetNextFibonacci(int beforePrevious, int previous)
{
int next = beforePrevious + previous;
Console.Write($"{next} ");
return beforePrevious = previous;
return previous = next;
}
}
}
Can anyone please help me to solve it?