Write a c program to find out the sum of G.P series

Sum of GP series in c programming language

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

int main(){

    float a,r,i,tn;
    int n;
    float sum=0;

    printf("Enter the first number 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);
       tn = a * (1 -pow(r,n-1));

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

    return 0;
}

Sample output:

Enter the first number of the G.P. series: 1
Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.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

No comments:

Post a Comment