C Program to Print the Fibonacci Series

C Program to Print the Fibonacci Series

A number-sequence exercise grows each term from the preceding two. Saving the new sum before replacing either term prevents a corrupted sequence.

Starting from zero and one, each Fibonacci term after them is the sum of the two preceding terms.

Before this exercise, review repeating work with loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why fibonacci sequence uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Print the first five terms instead of seven.

Fibonacci Sequence: follow the data

  1. Given data

    A number-sequence exercise grows each term from the preceding two. Saving the new sum before replacing either term prevents a corrupted sequence.

  2. Rule

    Starting from zero and one, each Fibonacci term after them is the sum of the two preceding terms.

  3. Observed result

    0 1 1 2 3 5 8

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

a holds the term to print; b holds the following term. Compute next = a + b before overwriting a or b.

The example asks for a term count, not a maximum term value. Printing seven terms includes the initial zero as the first term.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  unsigned a = 0, b = 1;
  for (int i = 0; i < 7; i++) {
    printf("%u ", a);
    unsigned next = a + b;
    a = b; b = next;
  }
  putchar('\n');
  return 0;
}

Expected output

0 1 1 2 3 5 8 
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  unsigned a = 0, b = 1;
  for (int i = 0; i < 7; i++) {
    printf("%u ", a);
    unsigned next = a + b;
    a = b; b = next;
  }
  putchar('\n');
  return 0;
}

The initial pair is (0, 1).

After printing zero, next is one and the pair becomes (1, 1). Later pairs produce two, three, five and eight.

Follow the worked calculation

  1. Start with the sample

    The initial pair is (0, 1).

  2. Apply the rule

    After printing zero, next is one and the pair becomes (1, 1). Later pairs produce two, three, five and eight.

  3. Check the result

    0 1 1 2 3 5 8

    0 1 1 2 3 5 8
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  unsigned a = 0, b = 1;
  for (int i = 0; i < 7; i++) {
    printf("%u ", a);
    unsigned next = a + b;
    a = b; b = next;
  }
  putchar('\n');
  return 0;
}

Print the first five terms instead of seven.

Worked answer

Only the iteration count changes. The two-value update rule still prints zero, one, one, two and three.

C17 · complete program

#include <stdio.h>
int main(void) {
  unsigned a = 0, b = 1;
  for (int i = 0; i < 5; i++) {
    printf("%u ", a);
    unsigned next = a + b;
    a = b; b = next;
  }
  putchar('\n');
  return 0;
}

Expected output

0 1 1 2 3 
Run in C compiler →

Recognize the important boundaries

The sequence grows quickly. Check a + b for overflow before updating when the term count is not a small fixed fixture.

Define whether your exercise starts with 0,1 or 1,1; otherwise an apparently different output may use a different convention.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why compute next before assigning a = b?
Choose one answer. You can try again.

Answer and explanation

The sum needs both original values.

Correct. Replacing a too early would lose one addend.

Use the idea independently

Starting from zero and one, each Fibonacci term after them is the sum of the two preceding terms.

Continue with testing the problem’s boundary cases.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Print A to Z Using a Loop →