Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 16

Sum of the geometric progression 1 + x + x² + ... + xⁿ

CExpression evaluationJNTUH R22 · CS108ES

Aim

To write a C program that reads two integers x and n and computes the sum of the geometric progression 1 + x + x² + x³ + ... + xⁿ, printing each term as well.

Algorithm

  1. Read x and n.
  2. If n is negative, print a message and stop.
  3. Set sum = 0 and term = 1 (this is x⁰).
  4. For i from 0 to n:
    • If i is more than 0, multiply term by x to get the next power, xⁱ.
    • Add term to sum and print it.
  5. Print the sum.

Program

C
#include <stdio.h>

int main(void) {
    int x, n;
    printf("Enter the values of x and n: ");
    if (scanf("%d %d", &x, &n) != 2) {
        printf("Invalid input\n");
        return 1;
    }
    if (n < 0) {
        printf("n cannot be negative\n");
        return 1;
    }

    long long sum = 0;
    long long term = 1;    /* x to the power 0 */
    printf("Sum = ");
    for (int i = 0; i <= n; i++) {
        if (i > 0) {
            term = term * x;    /* next power of x */
            printf(" + ");
        }
        sum = sum + term;
        printf("%lld", term);
    }
    printf(" = %lld\n", sum);
    return 0;
}

Output

x = 5, n = 3

Output 1
Enter the values of x and n: 5 3
Sum = 1 + 5 + 25 + 125 = 156

x = 2, n = 10

Output 2
Enter the values of x and n: 2 10
Sum = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024 = 2047

Negative n

Output 3
Enter the values of x and n: 3 -2
n cannot be negative

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

How it works

Each term is the previous term multiplied by x, so the program keeps one running term instead of calling pow for every power. For x = 5 and n = 3 the terms are 1, 5, 25 and 125, and the sum is 156.

The answer can be checked with the formula for a geometric progression: when x ≠ 1, the sum is (xⁿ⁺¹ − 1) / (x − 1). For x = 2 and n = 10 that is (2048 − 1) / 1 = 2047.

The values grow very quickly, so term and sum are long long (printed with %lld), which holds values up to about 9.2 × 10¹⁸. Even so, large x and n will overflow; for example 10¹⁹ is already too big.

Common mistakes

  • Starting the loop at i = 1 and forgetting the first term 1 (which is x⁰). The progression has n + 1 terms, from x⁰ to xⁿ.

  • Using x ^ i for the power. In C ^ is bitwise XOR, so the result is wrong without any warning.

  • Not checking for a negative n. The loop simply does not run and the program prints a sum of 0 as if it were correct.

  • Starting term at 0 instead of 1. Every later term is then 0 because 0 × x = 0.

Viva questions

Try to answer each one aloud before you open it.

  • What is a geometric progression?

    A sequence in which each term is the previous term multiplied by a fixed number called the common ratio. In 1, 5, 25, 125 the common ratio is 5.

  • How many terms are there in 1 + x + x² + ... + xⁿ?

    n + 1 terms, because the powers go from 0 to n.

  • What is the formula for the sum of this progression?

    When x ≠ 1 the sum is (xⁿ⁺¹ − 1) / (x − 1). When x = 1 every term is 1, so the sum is n + 1.

  • Why is the next term found by multiplying by x instead of calling pow?

    Each power is just the previous power times x, so one multiplication per term is enough. It is faster, uses only integers, and avoids the rounding of pow, which works with double.

  • What is the sum when x = 5 and n = 3?

    1 + 5 + 25 + 125 = 156.

Learn the concepts

All Programming for Problem Solving Lab experiments →