The numbers 2, 4, 6, 8 and 3, 9, 27, 81 follow rules. Once the rule is known, any number of further terms can be produced. Generating the terms of a sequence is a natural task for a loop.

Sequences and Terms

Definition: Sequence (of numbers). An ordered list of numbers, called terms, formed according to a definite rule.

The first term is usually written a, and the number of terms required is written n.

Arithmetic Progression

Definition: Arithmetic progression (AP). A sequence in which each term is obtained by adding a fixed number, the common difference d, to the term before it.

The sequence 3, 7, 11, 15 is an AP with a = 3 and d = 4. The n-th term is a + (n − 1)d, and the sum of the first n terms is n/2 × [2a + (n − 1)d].

Geometric Progression

Definition: Geometric progression (GP). A sequence in which each term is obtained by multiplying the term before it by a fixed number, the common ratio r.

The sequence 2, 6, 18, 54 is a GP with a = 2 and r = 3. The n-th term is a × r^(n − 1).

The Fibonacci Sequence

In the Fibonacci sequence the first two terms are 0 and 1, and every later term is the sum of the two terms before it: 0, 1, 1, 2, 3, 5, 8, 13 and so on. Unlike an AP or a GP, each new term depends on two earlier terms, so two values must be remembered. Table 2.7 compares the three sequences.

Table 2.7: Rules of three common sequences

Sequence Rule for the next term Example
Arithmetic progression Previous term + d 3, 7, 11, 15 (d = 4)
Geometric progression Previous term × r 2, 6, 18, 54 (r = 3)
Fibonacci Sum of the previous two terms 0, 1, 1, 2, 3, 5, 8

Generating Terms with a Loop

There is no need to use the formula for the n-th term in order to display a sequence. It is simpler to keep the current term in a variable and update it on every pass. For an AP the update is term = term + d; for a GP it is term = term * r.

For the Fibonacci sequence, two variables hold the last two terms. On each pass, the next term is computed as their sum, and then both variables are moved one step forward.

Suppose the two remembered terms are 2 and 3. Their sum, 5, is the next term. After it is displayed, the remembered terms become 3 and 5.

Other Sequences

The same method applies to any sequence with a clear rule. The squares 1, 4, 9, 16 are produced by printing i × i for i from 1 to n. The triangular numbers 1, 3, 6, 10 are produced by adding i to a running total.

Example 2.7: First terms of the Fibonacci sequence

Problem: Display the first 8 terms of the Fibonacci sequence.

Solution:

  1. Let a = 0 and b = 1 be the first two terms.
  2. Repeat 8 times: display a, compute next = a + b, then set a = b and b = next.
#include <stdio.h>

int main(void)
{
    int a = 0, b = 1, next, i, n = 8;

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

Output:

0 1 1 2 3 5 8 13

Result: The program uses only three variables for the terms, however many terms are required, because each new term depends only on the previous two.

Common mistakes

  • Updating a before computing the next term, which loses a value that is still needed.
  • Confusing the common difference of an AP with the common ratio of a GP.
  • Starting the loop counter at 0 while testing i <= n, which produces one term too many.

Key takeaways

  • An AP adds a common difference; a GP multiplies by a common ratio.
  • Each Fibonacci term is the sum of the two terms before it.
  • A loop generates a sequence by updating the current term on each pass.
  • Only as many earlier terms need be stored as the rule requires.

Practice questions

  1. Find the next term of the AP 5, 9, 13.
  2. Find the fifth term of the GP 2, 6, 18, 54.
  3. How many earlier terms must be remembered to produce the next Fibonacci term?

Answers to the practice questions

  1. 17, because the common difference is 4.
  2. 162, because each term is multiplied by 3.
  3. Two.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II