0
0
CsharpProgramBeginner · 2 min read

C# Program to Print Fibonacci Series

You can print the Fibonacci series in C# by using a loop with two variables to store the last two numbers and printing their sum repeatedly, like for (int i = 0, a = 0, b = 1; i < n; i++) { Console.Write(a + " "); int c = a + b; a = b; b = c; }.
📋

Examples

Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
🧠

How to Think About It

To print the Fibonacci series, start with the first two numbers 0 and 1. Then, repeatedly add the last two numbers to get the next one. Keep printing each number until you reach the desired count.
📐

Algorithm

1
Get the number of terms to print (n).
2
Initialize two variables with 0 and 1 for the first two Fibonacci numbers.
3
Use a loop to repeat n times:
4
Print the current number.
5
Calculate the next number by adding the previous two.
6
Update the two variables to move forward in the series.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int n = 10;
        int a = 0, b = 1;
        for (int i = 0; i < n; i++) {
            Console.Write(a + " ");
            int c = a + b;
            a = b;
            b = c;
        }
    }
}
Output
0 1 1 2 3 5 8 13 21 34
🔍

Dry Run

Let's trace printing the first 5 Fibonacci numbers through the code.

1

Initialize

a = 0, b = 1, i = 0

2

Print and Calculate

Print 0, c = 0 + 1 = 1, a = 1, b = 1, i = 1

3

Print and Calculate

Print 1, c = 1 + 1 = 2, a = 1, b = 2, i = 2

4

Print and Calculate

Print 1, c = 1 + 2 = 3, a = 2, b = 3, i = 3

5

Print and Calculate

Print 2, c = 2 + 3 = 5, a = 3, b = 5, i = 4

6

Print and Calculate

Print 3, c = 3 + 5 = 8, a = 5, b = 8, i = 5 (stop)

IterationabPrinted Number
0010
1111
2121
3232
4353
💡

Why This Works

Step 1: Start with first two numbers

We begin with a = 0 and b = 1 because the Fibonacci series starts with 0 and 1.

Step 2: Print current number

Each loop prints the current number stored in a before calculating the next.

Step 3: Calculate next number

The next Fibonacci number is the sum of the previous two, so we compute c = a + b.

Step 4: Update variables

We move forward by setting a = b and b = c to prepare for the next iteration.

🔄

Alternative Approaches

Recursive method
csharp
using System;
class Program {
    static int Fibonacci(int n) {
        if (n <= 1) return n;
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
    static void Main() {
        int n = 10;
        for (int i = 0; i < n; i++) {
            Console.Write(Fibonacci(i) + " ");
        }
    }
}
Simple but inefficient for large n due to repeated calculations.
Using array to store series
csharp
using System;
class Program {
    static void Main() {
        int n = 10;
        int[] fib = new int[n];
        fib[0] = 0;
        if (n > 1) fib[1] = 1;
        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }
        foreach (int num in fib) {
            Console.Write(num + " ");
        }
    }
}
Uses extra space but easy to access any Fibonacci number later.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs n times, so the time grows linearly with the number of Fibonacci numbers requested.

Space Complexity

Only a few variables are used, so space stays constant regardless of n.

Which Approach is Fastest?

The iterative loop method is fastest and most memory efficient compared to recursion or array storage.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Fast and memory efficient for printing series
Recursive methodO(2^n)O(n)Simple code but slow for large n
Array storageO(n)O(n)When you need to access all numbers later
💡
Use a loop with two variables to efficiently generate Fibonacci numbers without extra memory.
⚠️
Beginners often forget to update both variables correctly, causing an infinite loop or wrong output.