C program to Find the Sum of Geometric Progression Series

This is a C Program to find the sum of G.P series.

Problem Description

This C Program calculates the sum of G.P series.

Problem Solution

This program is used to find the sum of the geometric progression series. Here G.P stands for geometric progression. A geometric progression, or GP, is a sequence where each new term after the ? rest is obtained by multiplying the preceding term by a constant r, called the common ratio. The formula used in this program are Tn = a * (r ^ (n – 1)). where Tn is the last term of a finite sequence. Sn = a(1 – r ^ n + 1) / (1 – r) where Sn is the sum of n terms.

Program/Source Code

Here is source code of the C Program to Find the the sum of G.P series. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to Find the Sum of G.P Series
*/
#include <stdio.h>
#include <math.h>

int main()
{
float a, r, i, last_term, sum = 0;
int n;

printf("Enter the first term of the G.P. series: ");
scanf("%f", &a);
printf("Enter the total numbers in the G.P. series: ");
scanf("%d", &n);
printf("Enter the common ratio of G.P. series: ");
scanf("%f", &r);
sum = (a *(1 - pow(r, n + 1))) / (1 - r);
last_term = a * pow(r, n - 1);
printf("last_term term of G.P.: %f", last_term);
printf("\n Sum of the G.P.: %f", sum);
return 0;
}```
Program Explanation

In this program, we are reading the first term of the G.P. series using ‘a’ variable and the total numbers in the G.P. Series using ‘n’ variable and the common ratio of G.P series using ‘r’ variable.

A geometric progression, or GP, is a sequence where each new term after the first is obtained by multiplying the preceding term by a constant r, called the common ratio. The formula used in this program is

Tn = (a *(1-pow(r, n +1)))/ (1- r).

Where, Tn is the last term of a finite sequence.

Note: Join free Sanfoundry classes at Telegram or Youtube

Sn = a *(1-pow(r, n -1))

Where, Sn is the sum of n terms.

Runtime Test Cases
```
Output:
\$ cc pgm22.c -lm
\$ a.out
Enter the first term of the G.P. series: 3
Enter the total numbers in the G.P. series: 7
Enter the common ratio of G.P. series: 2
last_term term of G.P.: 192.000000
Sum of the G.P.: 765.000000```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.