print the fibonacci sequence with no recursion in csharp

To print the Fibonacci sequence without recursion in C#, we can use a simple loop-based iterative algorithm to generate the sequence. Here's an example implementation:

main.cs
int n = 10; // number of terms to generate
int prev = 0, next = 1;

for (int i = 0; i < n; i++)
{
    Console.Write(prev + " ");
    int temp = prev;
    prev = next;
    next += temp;
}
187 chars
11 lines

This code initializes two variables to represent the first two terms of the sequence (0 and 1), and then iterates a fixed number of times to generate and print subsequent terms. On each iteration, it adds the previous two terms together to generate the next term, and then re-assigns the variables to shift them over by one position in the sequence. Finally, it prints the current term to the console.

gistlibby LogSnag