0
0
CProgramBeginner · 2 min read

C Program to Print Fibonacci Series

A C program to print the Fibonacci series uses a loop to generate numbers where each number is the sum of the two previous ones, starting with 0 and 1. For example: int a=0, b=1, next; for(int i=0; i prints the first n Fibonacci numbers.
📋

Examples

Input5
Output0 1 1 2 3
Input1
Output0
Input10
Output0 1 1 2 3 5 8 13 21 34
🧠

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 as the first two Fibonacci numbers.
3
Repeat n times: print the current number, calculate the next by adding the last two, then update the last two numbers.
4
Stop when n numbers are printed.
💻

Code

c
#include <stdio.h>
int main() {
    int n = 10, a = 0, b = 1, next;
    for (int i = 0; i < n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    return 0;
}
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 variables

n=5, a=0, b=1

2

Iteration 1

Print a=0; next=0+1=1; a=1; b=1

3

Iteration 2

Print a=1; next=1+1=2; a=1; b=2

4

Iteration 3

Print a=1; next=1+2=3; a=2; b=3

5

Iteration 4

Print a=2; next=2+3=5; a=3; b=5

6

Iteration 5

Print a=3; next=3+5=8; a=5; b=8

Iterationa (printed)bnext
1011
2112
3123
4235
5358
💡

Why This Works

Step 1: Start with first two numbers

The Fibonacci series begins with 0 and 1, so we set a=0 and b=1.

Step 2: Print current number

We print the current number a in each loop iteration to show the series.

Step 3: Calculate next number

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

Step 4: Update variables

We then update a to b and b to next to move forward in the series.

🔄

Alternative Approaches

Recursive approach
c
#include <stdio.h>
int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}
int main() {
    int n = 10;
    for (int i = 0; i < n; i++) {
        printf("%d ", fib(i));
    }
    return 0;
}
This method is simple but inefficient for large n due to repeated calculations.
Using array to store series
c
#include <stdio.h>
int main() {
    int n = 10, fib[10];
    fib[0] = 0; fib[1] = 1;
    for (int i = 2; i < n; i++) {
        fib[i] = fib[i-1] + fib[i-2];
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", fib[i]);
    }
    return 0;
}
This stores all numbers but uses more memory; useful if you need to access series later.

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

Time Complexity

The loop runs n times, so the time complexity is O(n). Each iteration does constant work.

Space Complexity

Only a few variables are used to store numbers, so space complexity is O(1).

Which Approach is Fastest?

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

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Fast and memory efficient
RecursiveO(2^n)O(n)Simple code but slow for large n
Array storageO(n)O(n)When series needs to be reused
💡
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.