Write a C Program to find the Sum of the Geometric Progression Series (G.P. Series) using a Math Formula, and without using a Mathematical formula.
Geometric Progression Series
A geometric Series is a sequence of terms in which the next element is obtained by multiplying the common ratio by the previous element. Or G.P. Series is a series of numbers in which a common ratio of any consecutive numbers (items) is always the same.
The mathematical formula behind this Sum of G.P Series
Sn = a(rn) / (1- r)
Tn = ar(n-1)
C Program to find Sum of Geometric Progression Series Example
It allows the user to enter the first value, the total number of items in a series, and the common ratio. Next, it will find the sum of the Geometric Progression Series. Here, we used For Loop to display the G.P series, which is optional.
/* C Program to find Sum of Geometric Progression Series */ #include <stdio.h> #include<math.h> int main() { int a, n, r; float tn, sum = 0; printf(" Please Enter First Number of an G.P Series: "); scanf("%d", &a); printf(" Please Enter the Total Numbers in this G.P Series: "); scanf("%d", &n); printf(" Please Enter the Common Ratio: "); scanf("%d", &r); sum = (a * (1 - pow(r, n ))) / (1- r); tn = a * (pow(r, n - 1)); printf("\n The Sum of Geometric Progression Series = %.2f", sum); printf("\n The tn Term of Geometric Progression Series = %.2f \n", tn); return 0; }
Please Enter First Number of an G.P Series: 1
Please Enter the Total Numbers in this G.P Series: 5
Please Enter the Common Ratio: 2
The Sum of Geometric Progression Series = 31.00
The tn Term of Geometric Progression Series = 16.00
In this Program, we are not using any Mathematical formula.
/* C Program to find Sum of Geometric Progression Series */ #include <stdio.h> #include<math.h> int main() { int a, n, r, value, i; float sum = 0; printf(" Please Enter First Number of an G.P Series: "); scanf("%d", &a); printf(" Please Enter the Total Numbers in this G.P Series: "); scanf("%d", &n); printf(" Please Enter the Common Ratio: "); scanf("%d", &r); value = a; printf("G.P Series : "); for(i = 0; i < n; i++) { printf("%d ", value); sum = sum + value; value = value * r; } printf("\n The Sum of Geometric Progression Series = %f\n", sum); return 0; }
Please Enter First Number of an G.P Series: 2
Please Enter the Total Numbers in this G.P Series: 5
Please Enter the Common Ratio: 2
G.P Series : 2 4 8 16 32
The Sum of Geometric Progression Series = 62.000000
C Program to calculate the Sum of Geometric Progression Series using Functions
This geometric progression C Program is the same as above. However, we separated the C Programming logic using Functions
/* C Program to find Sum of Geometric Progression Series */ #include <stdio.h> #include<math.h> int sumofGP(int a, int n, int r) { int sum = (a * (1 - pow(r, n))) / (1- r); return sum; } int main() { int a, n, r; float sum = 0; printf(" Please Enter First Number of an G.P Series: "); scanf("%d", &a); printf(" Please Enter the Total Numbers in this G.P Series: "); scanf("%d", &n); printf(" Please Enter the Common Ratio: "); scanf("%d", &r); sum = sumofGP(a, n, r); printf("\n The Sum of Geometric Progression Series = %f\n", sum); return 0; }