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.

advertisement
advertisement

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.

advertisement

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.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.