Skip to study material
← Question index

QUESTION 28 · Worked algorithms

Develop an algorithm and flowchart to generate the first n Fibonacci terms.

5 marks

Answer in brief

A Fibonacci sequence begins with 0 and 1. Each later term is the sum of the two previous terms.

Algorithm

  1. Read the count: Accept n, the number of terms required.
  2. Set the starting values: Use a = 0, b = 1 and i = 0.
  3. Print a: It is the next required term. Increase i by 1.
  4. Update the pair if more terms are needed: First calculate next = a + b. Then set a = b and b = next.
  5. Repeat: Continue until exactly n terms have been printed.

Example: first seven terms

The output is 0, 1, 1, 2, 3, 5, 8. After printing 0, the pair becomes 1 and 1. After printing the first 1, it becomes 1 and 2. Each new pair is prepared using the old pair. The count n means the number of terms, not the largest value allowed in the sequence.

Need for the temporary sum

Both old values are needed to calculate the next term. If a is replaced by b before the sum is calculated, the old a is lost. Saving next = a + b first avoids this mistake. The program does not calculate an extra unused term after the last output.

Boundary cases

For n = 0, no terms are printed. For n = 1, only 0 is printed. For n = 2, the result is 0 and 1. Negative counts are invalid. The supplied program accepts counts from 0 to 93 so that the values fit its unsigned long long variables.

Time and space complexity

The loop prints n terms and performs at most n − 1 pair updates. Therefore time is O(n). Only the count, counter and a few sequence values are stored, so extra space is O(1). This is an iterative method using a loop, not a recursive method.

Reference table

↔ Scroll horizontally on a small screen
Boundary checks
CaseExpected behaviour
n = 0Print no values.
n = 1Print only 0.
n = 2Print 0 and 1.
n outside 0–93Reject in this fixed-width example.

Diagram or flowchart

Initialise the pair, output the current value while terms remain, and update the pair only when another output is required.
Generate exactly n Fibonacci terms · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    int n;
    if (scanf("%d", &n) != 1 || n < 0 || n > 93) {
        puts("Unsupported input"); return 1;
    }
    unsigned long long a = 0, b = 1;
    for (int i = 0; i < n; ++i) {
        printf("%s%llu", i == 0 ? "" : " ", a);
        if (i + 1 < n) {
            unsigned long long next = a + b;
            a = b; b = next;
        }
    }
    putchar('\n');
    return 0;
}

Sample input and output

Input

7

Output

0 1 1 2 3 5 8

Key points

  • Start with a = 0 and b = 1. Output the current a, then move to the next pair by computing the sum before overwriting either old value.
  • Calculate the next sum before replacing the old pair.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • September/October 2021 · Q1(a) · 7 marks

    What is flow chart? Draw a flowchart to generate Fibonacci sequence up to n numbers.

    Paper P01 · page 1
View all matching exam references →