Skip to lab content

UNIT 02 · Expression Evaluation

First n terms of the Fibonacci sequence

EXERCISE 02DC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

A Fibonacci sequence is defined as follows: the first and second terms are 0 and 1. Subsequent terms are found by adding the preceding two terms. Write a C program to generate the first n terms of the sequence.
Input format & conventions

The number of terms, from 1 to 93. This limit avoids overflowing unsigned long long in the values generated by this implementation.

UNDERSTAND THE IDEA

Explanation

Keep two consecutive terms: current starts at 0 and next starts at 1. Print current, then move both values forward.

The sequence here starts with F0 = 0. Thus the first 93 terms end at F92. Avoid calculating an extra term after printing the final requested value.

PLAN BEFORE CODING

Algorithm

  1. Read and validate the requested term count.
  2. Initialize current = 0 and next = 1.
  3. Print current.
  4. Unless this was the final term, calculate a temporary sum and shift the two values.
  5. Repeat until n terms are printed.

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for First n terms of the Fibonacci sequence: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>

int main(void) {
    int n;
    unsigned long long current = 0, next = 1;
    if (scanf("%d", &n) != 1 || n < 1 || n > 93) {
        puts("Invalid input.");
        return 1;
    }
    for (int i = 0; i < n; ++i) {
        printf("%s%llu", i == 0 ? "" : " ", current);
        if (i < n - 1) {
            unsigned long long sum = current + next;
            current = next;
            next = sum;
        }
    }
    putchar('\n');
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 fibonacci-sequence.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Before term 1current = 0; next = 1Print 0
Before term 2current = 1; next = 1Print 1
Before term 3current = 1; next = 2Print 1
Before term 4current = 2; next = 3Print 2

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
7
OUTPUT
0 1 1 2 3 5 8

Sample 2

INPUT
1
OUTPUT
0

Sample 3

INPUT
2
OUTPUT
0 1

Common mistakes

  • Use a temporary sum so one update does not destroy a value still needed.
  • Do not print n + 1 terms or generate beyond the supported numeric range.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(n); auxiliary space O(1).

Let n be the requested number of terms. The program prints each term once and updates two running values using one addition. It does not recursively recompute earlier terms.

There are n output iterations, so time is O(n). Only current, next, a temporary sum and counters are retained: O(1) auxiliary space. The 93-term bound prevents integer overflow; the estimate describes growth with the requested count.

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.