Header Ads

Arithmetic Progression in C: Print n Terms with Output

Updated 23 October 2026.

An arithmetic progression (AP) is a sequence whose consecutive terms differ by a constant value. To print its first n terms in C, start with the first term and add the common difference after each term, stopping after exactly n outputs.

For example, 3 5 7 9 11 is an AP with first term 3 and common difference 2. This program reads those values and the required number of terms from the keyboard.

Formula and inputs

i-th term = a + (i − 1) × d

Here, a is the first term, d is the common difference, and i starts at 1. The difference can be positive, zero, or negative. We will generate successive terms by addition rather than repeatedly evaluating the formula.

The program accepts decimal integers within the compiler’s int range. It allows 1–1,000 terms to keep console output manageable and checks that every requested term fits in an int.

C program

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int read_int(const char *prompt, int *result)
{
    char line[128], *end;
    long value;
    int extra;

    fputs(prompt, stdout);
    fflush(stdout);
    if (fgets(line, sizeof line, stdin) == NULL)
        return 0;
    if (strchr(line, '\n') == NULL) {
        extra = getchar();
        if (extra != EOF) {
            while (extra != '\n' && extra != EOF)
                extra = getchar();
            return 0;
        }
    }
    errno = 0;
    value = strtol(line, &end, 10);
    if (end == line || errno == ERANGE ||
        value < INT_MIN || value > INT_MAX)
        return 0;
    while (isspace((unsigned char)*end))
        ++end;
    if (*end != '\0')
        return 0;
    *result = (int)value;
    return 1;
}

int main(void)
{
    int a, d, n, i, term;

    if (!read_int("First term: ", &a) ||
        !read_int("Common difference: ", &d) ||
        !read_int("Number of terms (1-1000): ", &n)) {
        fputs("Error: enter one valid integer per line.\n", stderr);
        return EXIT_FAILURE;
    }
    if (n < 1 || n > 1000) {
        fputs("Error: number of terms must be 1 to 1000.\n", stderr);
        return EXIT_FAILURE;
    }

    /* Check every required addition before printing any terms. */
    term = a;
    for (i = 1; i < n; ++i) {
        if ((d > 0 && term > INT_MAX - d) ||
            (d < 0 && term < INT_MIN - d)) {
            fputs("Error: a term would exceed the int range.\n", stderr);
            return EXIT_FAILURE;
        }
        term += d;
    }

    term = a;
    fputs("AP: ", stdout);
    for (i = 0; i < n; ++i) {
        printf("%d%s", term, i + 1 == n ? "\n" : " ");
        if (i + 1 < n)
            term += d;
    }
    return EXIT_SUCCESS;
}

Sample input and output

First term: 3
Common difference: 2
Number of terms (1-1000): 5
AP: 3 5 7 9 11

Enter each answer on a separate line. With a=20, d=-5, and n=5, the output is:

AP: 20 15 10 5 0

With a=7, d=0, and n=4, every term is equal:

AP: 7 7 7 7

How the program works

  1. read_int reads a bounded line with fgets. It rejects overly long lines, missing input, out-of-range numbers, and extra text such as 5abc. strtol converts the text, and the remaining characters must be whitespace.
  2. The program rejects a term count outside 1–1,000. For n=1, the answer is simply the first term, whatever the difference.
  3. The first loop checks all required additions against INT_MIN and INT_MAX. If a term would exceed those limits, the program reports an error before printing a sequence.
  4. The second loop prints exactly n terms. It adds d only when another term is needed, so even a valid final term at an integer limit is handled correctly.

The two passes still take O(n) time, and the program uses O(1) extra space. The extra input checks make the example longer than a minimal loop, but prevent malformed input and overflowing integer additions.

Common mistakes and checks

Do not print the first term separately and then run a loop n more times: that produces too many terms. Also, while (n >= 0) runs once when n is zero. Use a counter from zero while it is less than n.

Check one-term output, zero difference, negative difference, invalid text, a zero term count, and integer limits. INT_MIN and INT_MAX are defined by limits.h; their numeric values depend on the implementation. An out-of-range sequence must report an error instead of wrapping around.

Try these exercises

  • Print six terms starting at -3 with a difference of 3. Predict the result before running the program.
  • Move the sequence-printing loop into a function that receives a, d, and n. Keep the validation checks.
  • Change the display so each term appears on its own line together with its position in the sequence.

No comments