C program to find out the sum of infinite G.P. series: geometric progression

Sum of infinite GP series in c programming language

#include<stdio.h>

int main(){

    float a,r;
    float sum=0;

    printf("Enter the first number of the G.P. series: ");
    scanf("%f",&a);

    printf("Enter the common ratio of G.P. series: ");
    scanf("%f",&r);

    if(1 > r)
         sum = a/(1-r);
    else
         sum = a/(r-1);

    printf("\nSum of the infinite G.P. series: %f",sum);

    return 0;
}

Sample output:

Enter the first number of the G.P. series: 1
Enter the common ratio of G.P. series: .5

Sum of the infinite G.P. series: 2.000000

Enter the first number of the G.P. series: 5
Enter the common ratio of G.P. series: 2

Sum of the infinite G.P. series: 5.000000

Definition of geometric progression (G.P.):

A series of numbers in which ratio of any two consecutive numbers is always a same number that is constant. This constant is called as common ratio.

Example of G.P. series:
                                             
2 4 8 16 32 64
Here common difference is 2 since ratio any two consecutive numbers for example 32 / 16 or 64/32 is 2.

Sum of G.P. series:
Sn =a(1–rn+1)/(1-r)

Tn term of G.P. series:

Tn = arn-1          

Sum of infinite G.P. series:

Sn = a/(1-r)  if 1 > r
   = a/(r-1)  if r > 1

6. Write a c program to find out the sum of given H.P.
Post a Comment