Program to find nth term of given Geometric Progression (original) (raw)

Last Updated : 31 Mar, 2026

Given three integers the first term **a, common ratio **r, and position **n, find the **n-th term of a geometric progression.
**Note: Since the result can be large, return the answer **modulo 10 9 + 7.

Examples:

Input: a = 2, r = 2, n = 4
Output: 16
Explanation: The GP series is 2, 4, 8, 16, ... The 4th term is 16.

Input: a = 4, r = 3, n=3
Output: 36
Explanation: The GP series is 4, 12, 36, 108,.. in which 36 is the 3rd term.

Try It Yourselfredirect icon

Approach 1: Using a Loop (Naive)

The n-th term can be computed manually by repeatedly multiplying the first term by the common ratio r, a total of n āˆ’ 1 times.

C++ `

#include using namespace std;

// Modulo value to prevent overflow const int M = 1000000007;

int nthTerm(int a, int r, int n) { int res = a;

// Multiply by common ratio (n - 1) 
// times: a * r^(n-1)
for (int i = 1; i < n; i++) {
    res = (1ll * res * r) % M;
}

return res;

}

int main() { int a = 2, r = 2, n = 4;

cout << nthTerm(a, r, n) << endl;

return 0;

}

Java

class GfG {

// Modulo value to prevent overflow
static final int M = (int)1e9 + 7;

static int nthTerm(int a, int r, int n) {
    int res = a;

    // Multiply by common ratio (n - 1) 
    // times: a * r^(n-1)
    for (int i = 1; i < n; i++) {
        res = (int)((1L * res * r) % M);
    }

    return res;
}

public static void main(String[] args) {
    int a = 2, r = 2, n = 4;

    System.out.println(nthTerm(a, r, n));
}

}

Python

Modulo value to prevent overflow

M = int(1e9 + 7)

def nthTerm(a, r, n): res = a

# Multiply by common ratio (n - 1) 
# times: a * r^(n-1)
for _ in range(1, n):
    res = (res * r) % M

return res

if name == "main": a = 2 r = 2 n = 4

print(nthTerm(a, r, n))

C#

using System;

class GfG { // Modulo value to prevent overflow const int M = 1000000007;

static int nthTerm(int a, int r, int n)
{
    int res = a;

    // Multiply by common ratio (n - 1) 
    // times: a * r^(n-1)
    for (int i = 1; i < n; i++)
    {
        res = (int)((1L * res * r) % M);
    }

    return res;
}

static void Main()
{
    int a = 2, r = 2, n = 4;

    Console.WriteLine(nthTerm(a, r, n));
}

}

JavaScript

// Modulo value to prevent overflow const M = 1e9 + 7;

function nthTerm(a, r, n) { let res = a;

// Multiply by common ratio (n - 1) times: a * r^(n-1)
for (let i = 1; i < n; i++) {
    res = (res * r) % M;
}

return res;

}

// Driver Code let a = 2, r = 2, n = 4; console.log(nthTerm(a, r, n));

`

Approach 2: Using Binary Exponentiation (Efficient)

The n-th term of a geometric progression, Tn = a Ɨ r^(nāˆ’1), can be found by raising the common ratio to the power (nāˆ’1). Instead of multiplying repeatedly, binary exponentiation smartly breaks the power into smaller parts using squaring, making the process much faster and efficient.

C++ `

#include using namespace std;

// Modulo value const int M = 1000000007;

// Function to compute (x^n) % M using binary exponentiation long long powMod(long long x, int n) { long long result = 1; x %= M;

while (n > 0) {
    if (n % 2 == 1)
        result = (result * x) % M;

    x = (x * x) % M;
    n /= 2;
}
return result;

}

// Function to compute the n-th term of // the geometric progression

int nthTerm(int a, int r, int n) { int power = powMod(r, n - 1); return (1LL * a * power) % M; }

// Driver Code int main() { int a = 2, r = 2, n = 4;

cout << nthTerm(a, r, n) << endl;

return 0;

}

Java

class GfG {

// Modulo value
static final int M = 1000000007;

// Function to compute (x^n) % M using 
// binary exponentiation
static int powMod(int x, int n) {
    long result = 1;
    long base = x % M;

    while (n > 0) {
        
        // If exponent is odd, multiply 
        // result with current base
        if ((n & 1) == 1) {
            result = (result * base) % M;
        }
        // Square the base
        base = (base * base) % M;
        n = n >> 1; // Equivalent to n = n / 2
    }

    return (int) result;
}

// Function to compute the n-th term 
// of the geometric progression

static int nthTerm(int a, int r, int n) {
    int power = powMod(r, n - 1);
    return (int)((1L * a * power) % M);
}

public static void main(String[] args) {
    int a = 2, r = 2, n = 4;

    System.out.println(nthTerm(a, r, n));
}

}

Python

Modulo value

M = int(1e9 + 7)

Function to compute (x^n) % M using binary exponentiation

def powMod(x, n): result = 1 x = x % M

while n > 0:
    
    # If exponent is odd, multiply 
    # result with current base
    if n % 2 == 1:
        result = (result * x) % M
    # Square the base
    x = (x * x) % M
    n //= 2

return result

Function to compute the n-th term

of the geometric progression

def nthTerm(a, r, n): power = powMod(r, n - 1) return (a * power) % M

if name == "main": a = 2 r = 2 n = 4

print(nthTerm(a, r, n))

C#

using System;

class GfG { // Modulo value const int M = 1000000007;

// Function to compute (x^n) % M 
// using binary exponentiation
static int powMod(int x, int n)
{
    long result = 1;
    long baseVal = x % M;

    while (n > 0)
    {
        // If exponent is odd, multiply result 
        // with current base
        if ((n & 1) == 1)
        {
            result = (result * baseVal) % M;
        }

        // Square the base
        baseVal = (baseVal * baseVal) % M;
        n >>= 1;
    }

    return (int)result;
}

// Function to compute the n-th term 
// of the geometric progression

static int nthTerm(int a, int r, int n)
{
    int power = powMod(r, n - 1);
    return (int)((1L * a * power) % M);
}

static void Main()
{
    int a = 2, r = 2, n = 4;

    Console.WriteLine(nthTerm(a, r, n));
}

}

JavaScript

// Modulo value const M = 1e9 + 7;

// Function to compute (x^n) % M // using binary exponentiation function powMod(x, n) { let result = 1; x = x % M;

while (n > 0) {
    
    // If exponent is odd, multiply 
    // result with current base
    if (n % 2 === 1) {
        result = (result * x) % M;
    }

    // Square the base
    x = (x * x) % M;
    n = Math.floor(n / 2);
}

return result;

}

// Function to compute the n-th term // of the geometric progression function nthTerm(a, r, n) { const power = powMod(r, n - 1); return (a * power) % M; }

// Driver Code const a = 2, r = 2, n = 4; console.log(nthTerm(a, r, n));

`