Beginner Number Problems C

Fibonacci Series Program in C with Output

Print the first n terms of the Fibonacci series in C using a loop and using recursion.

  • TimeO(n) for the loop version, about O(2^n) for the simple recursive version
  • SpaceO(1) for the loop, O(n) call stack for recursion
  • Hints3
  • Read3 min
Quick answer

The Fibonacci series starts with 0 and 1, and each next term is the sum of the two before it: 0 1 1 2 3 5 8 13 21 34. In C, keep two variables a and b, print a, compute next = a + b, then shift a = b and b = next inside a for loop. The loop version runs in O(n) time. The simple recursive version is shorter but takes exponential time.

for loopVariable update orderRecursionInteger overflow
1

Problem statement

Write a Fibonacci series program in C. Given n, print the first n terms of the series, starting from 0.

First use a loop, then use recursion. For n = 10 the output is 0 1 1 2 3 5 8 13 21 34.

Input

An integer n, the number of terms.

Output

The first n Fibonacci terms separated by spaces.

Examples

Example 1
Inputn = 10
Output0 1 1 2 3 5 8 13 21 34

0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, and so on.

Example 2
Inputn = 1
Output0

Only the first term is printed.

Constraints

  • 1 <= n <= 46 when terms are stored in int
  • Use long long for longer series
2

Think before you code

Reveal the questions to ask yourself first
  • Which two values do you need to make the next term?
  • After making the next term, which variable gets which value?
  • What does the series look like if you update a before saving next?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Start with a = 0 and b = 1.
2 Hint 2
Print a, then compute next = a + b before changing a or b.
3 Hint 3
For recursion, fib(0) = 0 and fib(1) = 1 are the base cases.
4

Approach

Reveal the step-by-step approach

Fibonacci series logic

Each term depends only on the two terms just before it.

Term index 0 1 2 3 4 5 6 7 8 9
Value 0 1 1 2 3 5 8 13 21 34

Fibonacci using loop in C

  1. Set a = 0, b = 1.
  2. Repeat n times: print a, set next = a + b, then a = b and b = next.

The order matters. If you write a = b first, the old a is lost and the sum is wrong.

Fibonacci using recursion

fib(n) = fib(n - 1) + fib(n - 2), with fib(0) = 0 and fib(1) = 1. Print fib(i) for i from 0 to n - 1. This matches the maths, but the same values are worked out again and again. See what is recursion for how the calls stack up.

5

Dry run

Walk through the example step by step

Loop version, first 5 terms:

i | print a | next = a + b | a after | b after
--+---------+--------------+---------+--------
1 | 0       | 1            | 1       | 1
2 | 1       | 2            | 1       | 2
3 | 1       | 3            | 2       | 3
4 | 2       | 5            | 3       | 5
5 | 3       | 8            | 5       | 8

Printed so far: 0 1 1 2 3.

6

Solution

Reveal the full C solution
solution.c
#include <stdio.h>

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

    printf("First %d Fibonacci terms:\n", n);
    for (int i = 1; i <= n; i++) {
        printf("%d ", a);
        int next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}

Output:

First 10 Fibonacci terms:
0 1 1 2 3 5 8 13 21 34

The loop runs exactly n times and prints one term each time. Only three int variables are used, whatever the value of n.

Fibonacci series in C using recursion

#include <stdio.h>

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

int main(void) {
    int n = 10;
    for (int i = 0; i < n; i++) {
        printf("%d ", fib(i));
    }
    printf("\n");
    printf("10th term (index 9) = %d\n", fib(9));
    return 0;
}
0 1 1 2 3 5 8 13 21 34
10th term (index 9) = 34

fib(n) <= 1 returns n itself, which covers both base cases. The output is the same, but each fib(i) call starts from scratch and makes two more calls. That is roughly O(2^n) time, so this version gets slow quickly as n grows. Use it to learn recursion, and use the loop for real work.

Sometimes the question is "print terms up to 100", not "print 10 terms". Change the loop condition:

#include <stdio.h>

int main(void) {
    int limit;
    printf("Enter the limit: ");
    scanf("%d", &limit);

    int a = 0, b = 1;
    while (a <= limit) {
        printf("%d ", a);
        int next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}
Enter the limit: 100
0 1 1 2 3 5 8 13 21 34 55 89

How far can int go?

Type Largest term that fits
int (32-bit) F(46) = 1836311903
long long (64-bit) F(92) = 7540113804746346429

F(47) = 2971215073 is larger than the int limit of 2147483647. For a longer series, change the variables to long long and print with %lld.

The Java version is on print the Fibonacci series up to n terms. For more lab programs, see C programs for beginners.

TimeO(n) for the loop version, about O(2^n) for the simple recursive version
SpaceO(1) for the loop, O(n) call stack for recursion
7

Review before the interview

Common mistakes

  • Updating a = b before computing next, which loses the old value of a.
  • Printing b instead of a, which skips the first 0.
  • Writing the base case as if (n == 1) return 1 only. fib(0) then never stops.
  • Using int for more than 46 terms. The values overflow and turn negative or wrong.

Edge cases to test

  • n = 1 prints only 0.
  • n = 2 prints 0 1.
  • n = 0 or negative: the loop does not run and nothing is printed. Check the input if needed.
  • Some books start the series at 1 1 2 3. Ask which start your teacher expects.

Interview follow-ups

  • How would you store the terms in an array and print them in reverse?
  • How can you make the recursive version fast with memoization?
  • How do you check whether a given number is a Fibonacci number?

Keep practising

Building your programming basics?

CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.

Chat with us