C Fibonacci Series Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is the Fibonacci Series?

The Fibonacci series starts with 0 and 1. Every next term is the sum of the previous two.

Remember
F(0) = 0
F(1) = 1
F(n) = F(n − 1) + F(n − 2)   for n ≥ 2

First terms:
  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Example (n = 7 terms):
  0  1  1  2  3  5  8
IndexTermHow
00seed
11seed
210 + 1
552 + 3

How to Print Fibonacci Series in C

Four clear steps — prefer an iterative loop for n terms.

  1. Choose how many terms n to print (n >= 1).
  2. Start with a = 0, b = 1.
  3. Print the current term, then set next = a + b, shift a = b, b = next.
  4. Repeat until n terms are printed (use long long for longer series).
MethodIdeaTimeBest for
Iterative loopKeep two previous terms; print and advanceO(n)Printing a series (preferred)
Recursive termfib(n) = fib(n-1) + fib(n-2)exponential if naiveTeaching the recurrence only

Pseudocode

Pseudocode
function PrintFibonacci(n):
    a = 0
    b = 1

    for i from 1 to n:
        print a
        next = a + b
        a = b
        b = next

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Seeds0, then 1
Next terma + b
Updatea = b; b = next
Type tipUse long long for longer series
AvoidNaive recursion for every printed term
Classic check7 terms → 0 1 1 2 3 5 8

Live Preview

Enter how many terms to print (1–30).

Whole numbers from 1 to 30. Tap a chip or type a count.

Result
n = 10 terms
0 1 1 2 3 5 8 13 21 34

Worked Walkthrough — 7 terms

Track a and b, print a, then slide the window forward.

Calculation
a=0, b=1

print 0 → next=0+1=1 → a=1, b=1
print 1 → next=1+1=2 → a=1, b=2
print 1 → next=1+2=3 → a=2, b=3
print 2 → next=2+3=5 → a=3, b=5
print 3 → next=3+5=8 → a=5, b=8
print 5 → next=5+8=13 → a=8, b=13
print 8

Series: 0 1 1 2 3 5 8

C Programs

Start with the iterative loop to print n terms. Use a small recursive helper only to show the recurrence for one term.

Example 1 — Iterative Series

Keep two previous values and print n terms with a loop.

C
#include <stdio.h>

int main(void)
{
    int n = 10;
    long long a = 0, b = 1, next;
    int i;

    if (n < 1)
    {
        printf("Enter n >= 1\\n");
        return 1;
    }

    for (i = 1; i <= n; i++)
    {
        printf("%lld", a);
        if (i < n)
        {
            printf(" ");
        }
        next = a + b;
        a = b;
        b = next;
    }
    printf("\\n");
    return 0;
}

How It Works

  1. Require n >= 1 so there is at least one term to print.
  2. Seed a = 0, b = 1.
  3. Each iteration prints a, then computes next = a + b.
  4. Shift: a = b, b = next — the window moves one step.
  5. Spaces go between terms only (i < n) so there is no trailing space.

Example 2 — Recursive Single Term

Show the recurrence for one index. Do not use this to print a long series.

C
#include <stdio.h>

long long fib(int n)
{
    if (n <= 0)
    {
        return 0;
    }
    if (n == 1)
    {
        return 1;
    }
    return fib(n - 1) + fib(n - 2);
}

int main(void)
{
    int i;
    for (i = 0; i < 7; i++)
    {
        printf("%lld", fib(i));
        if (i < 6)
        {
            printf(" ");
        }
    }
    printf("\\n");
    return 0;
}

How It Works

  1. Base cases: fib(0) = 0, fib(1) = 1.
  2. Otherwise return fib(n-1) + fib(n-2).
  3. The outer loop calls fib(i) for display — fine for tiny n.
  4. Each call tree recomputes shared subproblems, so cost grows fast.
  5. In interviews: explain recursion, then say you would print with Example 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

n = 1

Only 0

Print a single seed and stop.

Overflow

Use long long

Terms grow fast; int overflows early.

Naive recursion

Too slow for series

Do not call naive fib(i) for large counts.

Trailing space

Separate carefully

Print spaces between terms, not after the last one.

Time and Space Complexity

ProgramTimeExtra space
Iterative seriesO(n)O(1)
Naive recursive termexponential in the indexO(n) call stack

Printing n terms needs n advances of the a/b window. That is why iteration is the default answer.

Key Takeaways

  • Definition: each term is the sum of the previous two.
  • Loop: print a, then slide a/b forward.
  • Recursion: good for teaching F(n), not for long series.
  • Test: 7 terms → 0 1 1 2 3 5 8.

One line: start 0, 1 — then repeatedly replace the pair with (b, a+b).

Frequently Asked Questions

Each term is the sum of the two before it. With seeds 0 and 1, it begins 0, 1, 1, 2, 3, 5, 8, 13, …
A simple loop is O(n) time and O(1) extra space. Naive recursion recomputes the same terms and becomes exponential.
Yes. Some texts use F1 = F2 = 1. The recurrence is the same after the first values.
Fibonacci grows quickly. Prefer long long for longer series (typical int overflows well before 50 terms).
Iterative printing is O(n). Naive recursive fib(n) for each term is much slower — avoid that for a series.

Next: GCD

Find the greatest common divisor of two integers.

GCD tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful