In this tutorial you will learn about the C Program To find Sum of GP Series and its application with practical example.
In this tutorial, we will learn to create a C program that will find Sum of GP using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Basic C programming.
- C Operators.
- Basic input/output.
- Loops.
- For loop.
What Is geometric progression(GP)?
GP stands for geometric progression. It is defined as a Geometric Sequence of numbers each term is found by multiplying the previous term by a constant.
Example
This sequence has a factor of 2.Each term (except the first term) is found by multiplying the previous term by 2.
Generally we write a Geometric Sequence like that:
{a, ar, ar2, ar3, … }
where:
- a is the first term, and
- r is the factor between the terms (called “common ratio”).
C Program To find Sum of GP Series
In this program we will find sum of GP series using for loop. We would first declared and initialized the required variables. Next, we would prompt user to input the number of terms. Later we will generate the GP series of specified number of terms.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> #include<math.h> int main () { int f, no, cr, temp, i; //Variable Declaration float sum = 0; printf ("\nEnter First Number of an G.P Series: "); scanf ("%d", &f); //First term<strong> of</strong> series. printf ("\nEnter the Total Numbers in this G.P Series: "); scanf ("%d", &no); //Total Numbers of terms printf ("\nEnter the Common Ratio: "); scanf ("%d", &cr); //Common ratio temp = f; printf ("The G.P Series is : "); //Printing the series for (i = 0; i < no; i++) { printf ("%d ", temp); sum = sum + temp; //Update the sum in each iteration temp = temp * cr; } printf ("\nSum of G P Series is = %f\n", sum); return 0; } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- no = Total Numbers of terms to be printed
- f = First term of series.
- cr= common ratio.
- sum= adding values of GP.
- temp= holding temporary values.
- i= for iteration.
In the next statement user will be prompted to enter the number of terms which will be assigned to variable ‘no’ and first value in variable f and common ratio in cr.
Now ,We will loop through the iterator ‘i’ up to the number of terms.Inside the for loop we are printing the value of temp variable and adding the sum of series in variable sum. When loop ends we print the sum of given gp. So in that way we find the sum of given gp number.