print first 100 elements of fibonacci in csharp

To print the first 100 elements of the Fibonacci sequence in C#, we can use a loop and an array to store the sequence numbers. Here's an example code that does that:

main.cs
int[] fib = new int[100];
fib[0] = 0; // first Fibonacci number
fib[1] = 1; // second Fibonacci number

// calculate and store the rest of the sequence
for(int i = 2; i < 100; i++)
{
    fib[i] = fib[i-1] + fib[i-2];
}

// print the sequence
for(int i = 0; i < 100; i++)
{
    Console.Write(fib[i] + " ");
}
308 chars
16 lines

This code initializes an array fib of length 100, with the first two elements set to 0 and 1 respectively (as they are hardcoded). Then, a loop is used to calculate and store the rest of the Fibonacci sequence. Finally, another loop is used to print the first 100 elements of the sequence separated by spaces.

gistlibby LogSnag