C program to find the sum of the Geometric Progression (G.P.) series

Here, we are going to learn how to find the sum of the Geometric Progression (G.P.) series using C program?
Submitted by Nidhi, on August 01, 2021

Problem Solution:

Geometric Progression (GP):

A series of numbers is called a geometric progression (GP) series if the ratio of any two consecutive terms is always the same.

Example: 2 4 6 8 10 ...

If 'A' is the first term and 'R' is the common ratio, then:

  • nth term of a GP = A Rn-1
  • Geometric Mean = nth root of product of n terms in the GP
  • Sum of 'n' terms of a GP (R < 1) = [A (1 - Rn)] / [1 - R]
  • Sum of 'n' terms of a GP (R > 1) = [A (Rn - 1)] / [R - 1]
  • Sum of infinite terms of a GP (R < 1) = (A) / (1 - R)

In the below program, we will read the first term, the total number of terms, and a common ratio from user and print the last term and the sum of the G.P. series on the console screen.

Program:

The source code to find the sum of the Geometric Progression (G.P.) series is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the
// sum of Geometric Progression (G.P.) series

#include <stdio.h>
#include <math.h>

int main()
{

    int n = 0;

    float a = 0;
    float r = 0;
    float i = 0;
    float l = 0;
    float s = 0;

    printf("Enter the first term: ");
    scanf("%f", &a);

    printf("Enter the value of n: ");
    scanf("%d", &n);

    printf("Enter the common ratio: ");
    scanf("%f", &r);

    l = a * pow(r, n - 1);
    s = (a * (1 - pow(r, n + 1))) / (1 - r);

    printf("Last term of G.P. series: %f", l);
    printf("\nSum of the G.P. series  : %f\n", s);

    return 0;
}

Output:

RUN 1:
Enter the first term: 2
Enter the value of n: 10
Enter the common ratio: 2
Last term of G.P. series: 1024.000000
Sum of the G.P. series  : 4094.000000

RUN2:
Enter the first term: 2
Enter the value of n: 6
Enter the common ratio: 2.67
Last term of G.P. series: 271.385315
Sum of the G.P. series  : 1157.292725

RUN 3:
Enter the first term: 1
Enter the value of n: 5
Enter the common ratio: 2
Last term of G.P. series: 16.000000
Sum of the G.P. series  : 63.000000

Explanation:

Here, we read the first term, common ratio, and the total number of terms from the user. Then we found the last term and total of the G.P series and printed them on the console screen.

C Sum of Series Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.