Write a program that reads a number n and prints on the console the first n members of the Fibonacci sequence: https://en.wikipedia.org/wiki/Fibonacci_number (at a single line, separated by spaces) : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ….

Examples:

 n output: 1 0 3 0 1 1 10 0 1 1 2 3 5 8 13 21 34

edited

+1 vote

The task is very interesting :-)

Here is my solution - I wrote a lot of explanations in the comments - so you can read and understand how to solve it...

```using System;

class FibonacciNumbers
{
static void Main()
{

if (n == 0)
{
Console.WriteLine("Invalid input!");
}
else if (n == 1)
{
Console.WriteLine(0);
}

int x1 = 0;
int x2 = 1;
Console.Write("{0} {1} ", x1, x2);//we print the first 2 numbers of the Fibonacci sequence

for (int i = 2; i < n; i++)//we do the calculation for the numbers ABOVE 2
{
int x3 = x1 + x2;
Console.Write("{0} ", x3);//we use Console.Write INSTEAD OF Console.WriteLine - to glue the above Console.Write - the start of the Fibonacci
x1 = x2;
x2 = x3;
}
Console.WriteLine();
}
}```