Sum of Geometric Progression series

Last Updated : 22 May, 2026

Given na and r as the number of terms, first term and common ratio respectively of a Geometric Progression series. Find the sum of the series up to nth term.

The Geometric Progression series looks like :- a, ar, ar2, ar3, ar4, ..... 

Examples :

Input: n = 3, a = 3, r = 2
Output: 21
Explanation: Series up to 3rd term is 3 6 12, so sum will be 21.

Input : n=3, a = 1, r = 2
Output : 7
Explanation: Series up to 3rd term is 1 2 4, so sum will be 7.

Try It Yourself
redirect icon

[Naive Approach] Iterative Summation Method - O(N) Time and O(1) Space

A simple solution is to one by one add terms to calculate the sum of geometric series. 

Consider we have n = 5, a = 2 and r = 2.

Initializing sum = 0, and then iterating through all terms

  • i = 0: sum becomes 0 + 2 = 2, then a becomes 2 * 2 = 4
  • i = 1: sum becomes 2 + 4 = 6, then a becomes 4 * 2 = 8.
  • i = 2: sum becomes 6 + 8 = 14, then a becomes 8 * 2 = 16.
  • i = 3: sum becomes 14 + 16 = 30, then a becomes 16 * 2 = 32.
  • i = 4: sum becomes 30 + 32 = 62, then a becomes 32 * 2 = 64.
C++
#include <bits/stdc++.h>
using namespace std;

// function to calculate sum of
// geometric series
int sumOfGP(int n, int a, int r)
{
    int sum = 0;
    
    // iterate n times to generate and 
    // add each term in the progression
    for (int i = 0; i < n; i++)
    {
        sum = sum + a;

        // multiplying with r to get next term
        a = a * r;
    }
    return sum;
}

// driver function
int main()
{
    int a = 3;  
    int r = 2; 
    int n = 3;  

    cout << sumOfGP(n, a, r) << endl;
    return 0;
}
Java
import java.io.*;

class GFG{
    
    // function to calculate sum of 
    // geometric series
    static int sumOfGP(int n, int a, int r)
    {
        int sum = 0; 
        
        // iterate n times to generate and 
        // add each term in the progression
        for (int i = 0; i < n; i++)
        {
            sum = sum + a;
            
            // multiplying with r to get next term
            a = a * r;
        }
        return sum;
    }

    // driver function
    public static void main(String args[])
    {
        int a = 3; 
        int r = 2; 
        int n = 3 ; 
        
        System.out.printf(sumOfGP(n, a, r));
    }
    
}
Python
# function to calculate sum of 
# geometric series
def sumOfGP(n, a, r) :
    
    sum = 0
    i = 0
    
    # iterate n times to generate and
    # add each term in the progression
    while i < n :
        sum = sum + a
        
        # multiplying with r to get next term
        a = a * r
        i = i + 1
    
    return sum
    
#driver function

a = 3 
r = 2 
n = 3 
        
print(sumOfGP(n, a, r)),
    
C#
using System;

class GFG {
    
    // function to calculate 
    // sum of geometric series
    static int sumOfGP(int n, int a, int r)
    {
        int sum = 0; 
        
        // iterate n times to generate and 
        // add each term in the progression
        for (int i = 0; i < n; i++)
        {
            sum = sum + a;
            
            // multiplying with r to get next term
            a = a * r;
        }
        return sum;
    }

    // Driver Code
    static public void Main ()
    {
        
        int a = 3; 
        int r = 2;
        int n = 3; 
        
        Console.WriteLine((sumOfGP(n, a, r)));
    }
    
}
JavaScript
// function to calculate sum
// of geometric series
function sumOfGP(n, a, r) {
    let sum = 0;
    
    // iterate n times to generate and 
    // add each term in the progression
    for (let i = 0; i < n; i++) {
        sum = sum + a;
        
        // multiplying with r to get next term
        a = a * r;
    }
    return sum;
}

// Driver code
let a = 3;  
let r = 2; 
let n = 3;  

console.log(sumOfGP(n, a, r))

Output
21

[Expected Approach] Direct Formula Method - O(log(n)) Time and O(1) Space

An efficient solution to solve the sum of geometric series where first term is a and common ration is r is by the formula :- sum of series = a(1 - rn)/(1 - r). Where r = T2/T1 = T3/T2 = T4/T3 . . . Here T1, T2, T3, T4 . . . ,Tn are the first, second, third, . . . ,nth terms respectively.

sumofthenthtermofGP

Consider we have n = 5, a = 2 and r = 2.

Series is 2, 4, 8, 16, 32 which sum up to 62 = 2 * (25 - 1) / (2 - 1).

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

// function to calculate sum of
// geometric series
int sumOfGP(int n, int a, int r)
{
    
    // if common ratio is 1, the series 
    // is simply a, a, a, ...(n times)
    if (r == 1)
    {
        return a * n;
    }

    // applying standard geometric series 
    // formula : a * (r^n - 1) / (r - 1)
    return (a * (pow(r, n) - 1)) / (r - 1);
}

// driver code
int main()
{
    int a = 3;  
    int r = 2;  
    int n = 3;  

    cout << sumOfGP(n, a, r);
    return 0;
}
Java
import java.math.*;

class GFG {

    // function to calculate sum of
    // geometric series
    static int sumOfGP(int n, int a, int r)
    {
        
        // if common ratio is 1, the series 
        // is simply a, a, a, ...(n times)
        if (r == 1) {
            return a * n;
        }
    
        // applying standard geometric series 
        // formula : a * (r^n - 1) / (r - 1)
        return (a * (int)(1 - (Math.pow(r, n)))) / (1 - r);
    }

    // driver code
    public static void main(String args[])
    {
        int a = 3;  
        int r = 2; 
        int n = 3; 

        System.out.println((sumOfGP(n, a, r)));
    }
}
Python
# function to calculate sum of 
# geometric series
def sumOfGP(n, a, r) :
    
    # if common ratio is 1, the series 
    # is simply a, a, a, ...(n times)
    if r == 1:
        return a * n

    # applying standard geometric series 
    # formula : a * (r^n - 1) / (r - 1) 
    return (a * (pow(r, n) - 1)) // (r - 1)
    
    
# driver code
a = 3  
r = 2  
n = 3  

print sumOfGP(n, a, r)
C#
using System;

class GFG {
    
    // function to calculate sum of 
    // geometric series
    static int sumOfGP(int n, int a, int r)
    {
        
        // if common ratio is 1, the series 
        // is simply a, a, a, ...(n times)
        if (r == 1) {
            return a * n;
        }
    
        // applying standard geometric series 
        // formula : a * (r^n - 1) / (r - 1)
        return (a * (1 - (Math.Pow(r, n)))) / (1 - r);
    }

    // Driver Code
    public static void Main()
    {
        int a = 3;  
        int r = 2;  
        int n = 3;  

        Console.Write(sumOfGP(n, a, r));
    }
}
JavaScript
// function to calculate sum of
// geometric series
function sumOfGP(n, a, r)
{

    // if common ratio is 1, the series is
    // simply a, a, a,...(n times)
    if (r == 1) {
        return a * n;
    }

    // applying standard geometric series
    // formula : a * (r^n - 1) / (r - 1)
    return (a * (Math.pow(r, n)) - 1) / (r - 1);
}

// Driver code
function main()
{
    let a = 3;
    let r = 2;
    let n = 3;

    console.log(sumOfGP(n, a, r));
}

// Run the main function
main();

Output
21


Comment