Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 12

First n terms of the Fibonacci sequence

CExpression evaluationJNTUH R22 · CS108ES

Aim

To write a C program that reads n and prints the first n terms of the Fibonacci sequence, where the first two terms are 0 and 1 and each later term is the sum of the two terms before it.

Algorithm

  1. Read n. If n is less than 1 or more than 47, print a message and stop.
  2. Set first = 0 and second = 1.
  3. Print first. If n is at least 2, also print second.
  4. For i from 3 to n:
    • Find next = first + second and print it.
    • Move forward: first = second, second = next.
  5. Stop.

Program

C
#include <stdio.h>

int main(void) {
    int n;
    printf("Enter the number of terms: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    if (n < 1 || n > 47) {
        printf("Enter a number of terms from 1 to 47\n");
        return 1;
    }

    int first = 0, second = 1;
    printf("Fibonacci sequence:\n");
    printf("%d ", first);           /* term 1 */
    if (n >= 2) {
        printf("%d ", second);      /* term 2 */
    }
    for (int i = 3; i <= n; i++) {
        int next = first + second;  /* sum of the two previous terms */
        printf("%d ", next);
        first = second;
        second = next;
    }
    printf("\n");
    return 0;
}

Output

10 terms

Output 1
Enter the number of terms: 10
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 

Only 1 term

Output 2
Enter the number of terms: 1
Fibonacci sequence:
0 

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The first two terms are fixed, so they are printed before the loop. The loop then makes terms 3 to n, each from the two terms before it, and moves the pair (first, second) one step along the sequence. The check n >= 2 makes sure that only one term is printed when n is 1.

The limit of 47 terms is because of the size of int. The 47th term is 1836311903, which fits in a 32-bit int (maximum 2147483647), but the 48th term, 2971215073, does not. For more terms, use long long and print it with %lld.

Common mistakes

  • Printing 0 and 1 before the loop and then running the loop n times. This prints n + 2 terms. Since two terms are already printed, the loop must make only terms 3 to n.

  • Updating in the wrong order, for example first = second; second = first + second;. By the time the sum is found, first has already changed, so the sequence goes wrong. Save the sum in next first.

  • Not limiting n. Beyond 47 terms the values no longer fit in an int and negative or wrong numbers are printed.

  • Starting the sequence at 1, 1. This lab defines the first two terms as 0 and 1.

Viva questions

Try to answer each one aloud before you open it.

  • What is the Fibonacci sequence?

    A sequence where the first two terms are 0 and 1 and every later term is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

  • Why is a third variable next needed?

    The new term depends on both old terms. If first were changed before the sum is found, its old value would be lost. next holds the sum while the two variables move forward.

  • What is the 10th term of the sequence when it starts with 0 and 1?
    1. The terms are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
  • Why does the program allow at most 47 terms?

    The 48th term, 2971215073, is larger than the largest value a 32-bit int can hold (2147483647), so it would overflow.

  • Can the Fibonacci sequence be generated with recursion?

    Yes, with fib(n) = fib(n − 1) + fib(n − 2), fib(0) = 0 and fib(1) = 1. But simple recursion repeats the same calculations many times, so the loop is much faster.

Learn the concepts

All Programming for Problem Solving Lab experiments →