Program to find nth term of given Geometric Progression
Last Updated :
14 Jul, 2025
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 109 + 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.
[Naive Approach] - Using a for Loop - O(n) Time and O(1) Space
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 <iostream>
using namespace std;
// Modulo value to prevent overflow
const int M = 1e9 + 7;
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));
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 <iostream>
using namespace std;
// Modulo value
const int M = 1000000007;
// Function to compute (x^n) % M using binary exponentiation
int powMod(int x, int n) {
int result = 1;
x = x % M;
while (n > 0) {
// If exponent is odd, multiply result with
// current base and take modulo
if (n % 2 == 1) {
result = (1LL * result * x) % M;
}
// Square the base and take modulo
x = (1LL * x * x) % M;
// Divide the exponent by 2
n = 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));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem