In this post, you will learn to write a program to compute sum of geometric progression.

A geometric progression is a sequence of numbers where each term is obtained by multiplying the previous term by a fixed number, called the common ratio.

The program receive two numbers and compute the sum of geometric progression.

How does the program works?

The program does the following:

  1. Implements the geometric progression ,
  2. Compute the input values when it receives it,
  3. Output the results.

Problem Definition

A geometric progression has two conditions.

  1. \Large a_1 = a where \Large a is the first term and not equal to \Large 0.
  2. \Large a_n = a_{n-1}r for \Large n \geq 1

\Large r is called the common ratio.

\Large \begin{aligned} &Suppose \hspace{2px} a = 1 \hspace{4px} and\hspace{4px}  r = 5, \hspace{2px} then\\ \\&a_1=1\\ &a_2= 5,\\&a_3=25\\& a_4= 125\end{aligned}

Basically, you can compute any term using the following formula.

\Large \begin{aligned}a_n \hspace{1ex} = \hspace{1ex} a \cdot \hspace{1ex} r_{n-1}\end{aligned}

The summation of a geometric progression is given below.

 \Large \begin{aligned} 1\hspace{1ex} + \hspace{1ex} x \hspace{1ex} + \hspace{1ex} x^2\hspace{1ex} + \hspace{1ex} x^3 \hspace{1ex} + \hspace{1ex} x^4 \hspace{1ex} + \hspace{1ex} \cdots \hspace{1ex} + \hspace{1ex} x^{n-1} \hspace{1ex} \cdots\end{aligned}

The summation can be translated into a program for any given \Large x value and \Large n value.

The program for geometric series is given below.

Program Source Codes

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int geo_sum, i, x,n;
    geo_sum = 1;
    printf("Enter the value for x:");
    scanf("%d",&x);
    printf("Enter the value for n:");
    scanf("%d",&n);
if(n <= 0 || x <= 0)
{
    printf("Incorrect Value!!\n\n");
}
else
{
    printf("The value is valid!!\n\n");
}
for(i = 1;i <= n; i++)
{
    geo_sum = geo_sum + pow(x,i);
}
printf("The Sum of Series = %d\n",geo_sum);
system("PAUSE"); 
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int x, n;
    long long geo_sum = 1;

    cout << "Enter the value for x: ";
    cin >> x;

    cout << "Enter the value for n: ";
    cin >> n;

    if (n <= 0 || x <= 0)
    {
        cout << "Incorrect Value!!" << endl;
        return 0;
    }
    else
    {
        cout << "The value is valid!!" << endl;
    }

    for (int i = 1; i <= n; i++)
    {
        geo_sum += pow(x, i);
    }

    cout << "The Sum of Series = " << geo_sum << endl;
    return 0;
}
import java.util.Scanner;

class GeometricSeries
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int x, n;
        long geo_sum = 1;

        System.out.print("Enter the value for x: ");
        x = sc.nextInt();

        System.out.print("Enter the value for n: ");
        n = sc.nextInt();

        if (n <= 0 || x <= 0)
        {
            System.out.println("Incorrect Value!!");
            return;
        }
        else
        {
            System.out.println("The value is valid!!");
        }

        for (int i = 1; i <= n; i++)
        {
            geo_sum += Math.pow(x, i);
        }

        System.out.println("The Sum of Series = " + geo_sum);
        sc.close();
    }
}
import math

geo_sum = 1

x = int(input("Enter the value for x: "))
n = int(input("Enter the value for n: "))

if n <= 0 or x <= 0:
    print("Incorrect Value!!")
else:
    print("The value is valid!!")
    for i in range(1, n + 1):
        geo_sum += math.pow(x, i)

    print("The Sum of Series =", int(geo_sum))

Output

The input values for the program is \Large r = x = 2 and \Large n = 10. If we use the values in the geometric expression as follows.

1 + 2 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29
= 1  + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024
= 2047

Therefore, the output is correct.

Enter the value for x:2
Enter the value for n:10
The value is valid!!
The Sum of Series = 2047
Press any key to continue . . . _