# C Program To Sum of a G.P Series

GP stands for **geometric progression**. It is defined as a sequence of numbers where each succeeding term is generated by multiplying each preceding term with a constant value.

For instance, 2, 4, 8,16, .... are in GP as the succeeding value is obtained by multiplying a constant value 2. This constant difference is often referred to as the common ratio.

We denote the first term of the series as **a**, last term as **tn**, the total number of elements as **n**, and the common ratio as **r**.

In this tutorial, we are given a geometric series and our task is to find the sum of that series. But before moving forward, if you are not familiar with the concept of loops in C, then do check the article on Loops in C.

**Input:** Enter the series: 3 6 12 24 48

**Output: **The sum of the G.P series is 288.00

## Program 1: Sum of a G. P. Series

In this program, we will find the sum of a geometric series without using both formulas and functions. Firstly, the first term, the total number of terms, and the common ratio are declared. Then, we declare two variables; one for sum and the other for the number. In each iteration we will keep on updating both the elements and at the end will print the result.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common ratio.
- Use a for loop that will calculate the sum.
- Declare two variables for sum and element.
- Update both the elements in each iteration
- At the end display the calculated sum.
- Stop

The below program demonstrates how to calculate the sum of a G.P series using for loop without using a formula. Here, we will update both the sum and element in each iteration

```
/* C Program to find Sum of Geometric Progression Series without using formula */
#include <stdio.h>
#include<math.h>
int main ()
{
int a, n, r, temp, i; //Variable Declaration
float sum = 0; //Sum declaration and initialization
printf ("\nEnter First Number of an G.P Series: ");
scanf ("%d", &a); //First term initialization
printf ("\nEnter the Total Numbers in this G.P Series: ");
scanf ("%d", &n); //Total Numbers of terms initialization
printf ("\nEnter the Common Ratio: ");
scanf ("%d", &r); //Common term initialization
temp = a;
//Print the series
printf ("The G.P Series is : ");
for (i = 0; i < n; i++)
{
printf ("%d ", temp);
sum = sum + temp; //Update the sum in each iteration
temp = temp * r; //Update the term in each iteration
}
//Print the sum
printf ("\nThe Sum of Geometric Progression Series = %f\n", sum);
return 0;
}
```

Enter First Number of an G.P Series: 2

Enter the Total Numbers in this G.P Series: 10

Enter the Common Ratio: 2

The G.P Series is : 2 4 8 16 32 64 128 256 512 1024

The Sum of Geometric Progression Series = 2046.000000

## Program 2: Sum of a G. P. Series

In this program, we will find the sum of a geometric series using a for loop. Firstly, the first term, the total number of terms, and the common ratio are declared. Then, we calculate the total sum of the geometric series using the formula and print it using the for loop.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common ratio.
- Use a for loop that will calculate the sum.
- Declare the formula for sum and last term before the loop.
- Calculate the sum till the last element in the for loop.
- Display the sum.
- Stop

The below program demonstrates how to calculate the sum of a GP series using for loop.

```
/* C Program to find Sum of Geometric Progression Series */
#include <stdio.h>
#include<math.h>
int main()
{
int a, n, r; //Variable Declaration
float tn, sum = 0; //Sum declaration and initialization
printf("\nEnter First Number of an G.P Series: ");
scanf("%d", &a); //First term initialization
printf("\nEnter the Total Numbers in this G.P Series: ");
scanf("%d", &n); //Total Number of terms initialization
printf("\nEnter the Common Ratio: ");
scanf("%d", &r); //Common difference initialization
sum = (a * (1 - pow(r, n ))) / (1- r); //Formula for sum
tn = a * (pow(r, n - 1)); //Formula for last term
printf("\n The Sum of Geometric Progression Series = %.2f", sum);
printf("\n The tn Term of Geometric Progression Series = %.2f \n", tn);
return 0;
}
```

Enter First Number of an G.P Series: 2

Enter the Total Numbers in this G.P Series: 10

Enter the Common Ratio: 2

The G.P Series is : 2 4 8 16 32 64 128 256 512 1024

The Sum of Geometric Progression Series = 2046.00

The tn term of Geometric Progression Series = 1024.00

## Program 3: Sum of a G. P. Series

In this method, we will find the sum of a geometric series using both formulas and functions. Firstly, the first term, the total number of terms, and the common ratio are declared. Then, a function is called to calculate the total sum of the geometric series.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common ratio.
- Call the function that will calculate the sum.
- Declare the formula for sum and last term in the function.
- Calculate the sum till the last element.
- Display the sum.
- Stop

The below program demonstrates how to calculate the sum of a G.P series using both functions and formulas.

```
/* C Program to find Sum of Geometric Progression Series */
#include <stdio.h>
#include<math.h>
int findSum(int a, int n, int r); //Function Declaration
int main()
{
int a, n, r; //Variable Declaration
float sum = 0; //Sum declaration and initialization
printf("\nEnter First Number of an G.P Series: ");
scanf("%d", &a); //First term initialization
printf("\nEnter the Total Numbers in this G.P Series: ");
scanf("%d", &n); //Total Number of terms initialization
printf("\nEnter the Common Ratio: ");
scanf("%d", &r); //Common ratio initialization
float tn = a * (pow(r, n - 1)); //Formula for last term
sum = findSum(a, n, r); //Function Call
//Print the series
printf("The G.P series is : \n");
printf("%d ",a);
for(int i=1;i<n;i++)
{
a=a*r;
printf("%d ",a);
}
//Print the sum
printf("\n The Sum of Geometric Progression Series = %.2f\n", sum);
//Print the last term
printf("\n The tn Term of Geometric Progression Series = %.2f \n", tn);
return 0;
}
int findSum(int a, int n, int r) //Function Definition
{
int sum = (a * (1 - pow(r, n))) / (1- r); //Formula for sum
return sum; //Return Sum
}
```

Enter First Number of an G.P Series: 3

Enter the Total Numbers in this G.P Series: 10

Enter the Common Ratio: 2

The G.P series is :

3 6 12 24 48 96 192 384 768 1536

The Sum of Geometric Progression Series = 3069.00

The tn Term of Geometric Progression Series = 1536.00