Print the first 10 Fibonacci numbers starting with 0 and 1.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
int a = 0, b = 1;
Console.Write(a + " ");
for (int i = 2; i < 10; i++)
{
int c = a + b;
Console.Write(c + " ");
a = b;
b = c;
}
}
}
Try solving on your own first, then reveal the official answer.
Each term is sum of previous two. Interviewers often ask iterative vs recursive versions.