Sum of a Geometric Progression in C++



What is Geometric Progression?

A geometric progression is a sequence of numbers where each term is obtained by multiplying the previous term by a fixed number. This fixed number is known as the common ratio and it should be a non-zero.

In this article, we are going to learn how we can find the sum of given n terms in geometric progression in C++.

We are given first-term, common ratio, and number of terms for which we have to find the sum of geometric progression.

Formula for Calculating Sum of Geometric Progression

Sn= a (1 - rn)/(1 - r) 
  1. where Sn is the sum of geometric progression (GP).
  2. a is the first term of GP.
  3. r is the common ratio of GP.
  4. n is the number of terms for which we are finding the sum.

Special Case

If the value of the common ratio is 1 i.e. r =1 then the formula for Calculating the Sum of GP:

Sn = n * a
  1. where n is the number of terms.
  2. a is the first term.

Example 1

  • Input: a = 2, r = 3, n = 4
  • Output: 80

Explanation

As input is given First term "a" starts from 2, the common ratio "r" equals 3, and the number of terms "n" equals 4, which means it will go till the 4th term of a sequence.
So it will give GP series: 2, 6, 18, 54. By adding all these four terms 2 + 6 + 18 + 54 will result in 80, therefore will get an output of 80.

Example 2

  • Input: a = 5, r = 0.5, n = 3
  • Output: 8.75

Explanation

As input is given First term "a" starts from 5, the common ratio "r" equals 0.5, and the number of terms "n" equals 3, which means it will go till the 3rd term of a sequence.
So it will give GP series: 5, 2.5, 1.25, By adding all these four terms 5 + 2.5 + 1.25 will result in 8.75, therefore will get an output of 8.75.

Direct Formula Approach

We can use a direct formula approach to calculate the sum of n terms of geometric progression. We take the number of terms, first term, and common ratio as input. We put all values in the formula to calculate the sum of geometric progression.

Steps for Implementation

  1. Take input "a" as the first term, r as the common ratio, and n as the number of terms.
  2. Use the general GP formula to calculate the sum.
  3. The formula for the sum of n terms of GP: Sn =a (1 - rn)/(1 - r)
  4. Return the calculated sum.

Implementation Code

#include<bits/stdc++.h>
using namespace std;

double sumOfGP(int a, int r, int n) {
  if (r == 1) {
    return n * a;
  }
  return a * (1 - pow(r, n)) / (1 - r);
}
int main() {
  double a = 2;
  double r = 2;
  int n = 5;
  double ans = sumOfGP(a, r, n);
  cout << "The sum of the geometric progression is: " << ans << endl;

}

Output

The sum of the geometric progression is: 62

Time Complexity: O(1), constant time as we are using the direct formula.
Space Complexity: O(1), constant space.

Iterative Approach

In this approach we take input a: First term of the GP, r: Common ratio of the GP, and n: Number of terms in the GP. Start with a variable to store the sum and iterate from 1 to n. In each iteration add the current term to sum and update the term by multiplying it with r to calculate the next term in the sequence. When all terms are added, return the value of the final sum.

Steps for Implementation

  1. Create a function to calculate the sum of n terms of GP iteratively.
  2. Initialize a variable sum to store the sum of GP and start with the first term.
  3. Inside the loop, add the current term to the sum and move to the next term.
  4. Return the sum after iterating through the loop completely till n terms.

Implementation Code

#include <bits/stdc++.h>
using namespace std;

// Function to calculate the sum of a geometric progression
double sumOfGPIterative(double a, double r, int n) {
    double sum = 0;
    double term = a;

    for (int i = 0; i < n; i++) {
        sum += term;  
        term *= r;   
    }
    return sum;
}

int main() {
    double a = 2;  // First term
    double r = 2;  // Common ratio
    int n = 5;     // Number of terms
    double result = sumOfGPIterative(a, r, n);
    cout << "The sum of the geometric progression is: " << result << endl;
    return 0;
}

Output

The sum of the geometric progression is: 62

Time Complexity: O(n), as it iterates through n terms.
Space Complexity: O(1), no extra space used.

Updated on: 2024-12-11T09:50:54+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements