Given first term (a), common ratio (r), and an integer N of the Geometric Progression series, the task is to find the Nth term of the series.
Examples:
Input: a = 2 r = 2, N = 4
Output: The 4th term of the series is : 16Input: a = 2 r = 3, N = 5
Output: The 5th term of the series is : 162
Approach: To solve the problem follow the below idea:
We know the Geometric Progression series is like = 2, 4, 8, 16, 32 …. …
In this series 2 is the stating term of the series .
Common ratio = 4 / 2 = 2 (ratio common in the series).
so we can write the series as :t1 = a1
t2 = a1 * r(2-1)
t3 = a1 * r(3-1)
t4 = a1 * r(4-1)
.
.
.
.
tN = a1 * r(N-1)
To find the Nth term in the Geometric Progression series we use the simple formula as shown below as follows:
TN = a1 * r(N-1)
Below is the implementation of the above approach:
// CPP Program to find nth term of // geometric progression #include <bits/stdc++.h> using namespace std;
int Nth_of_GP( int a, int r, int N)
{ // using formula to find
// the Nth term
// TN = a1 * r(N-1)
return (a * ( int )( pow (r, N - 1)));
} // Driver code int main()
{ // starting number
int a = 2;
// Common ratio
int r = 3;
// N th term to be find
int N = 5;
// Function call
cout << "The " << N << "th term of the series is : "
<< Nth_of_GP(a, r, N);
return 0;
} |
// java program to find nth term // of geometric progression import java.io.*;
import java.lang.*;
class GFG {
public static int Nth_of_GP( int a, int r, int N)
{
// using formula to find the Nth
// term TN = a1 * r(N-1)
return (a * ( int )(Math.pow(r, N - 1 )));
}
// Driver code
public static void main(String[] args)
{
// starting number
int a = 2 ;
// Common ratio
int r = 3 ;
// N th term to be find
int N = 5 ;
// Function call
System.out.print( "The " + N + "th term of the"
+ " series is : "
+ Nth_of_GP(a, r, N));
}
} |
# Python3 Program to find nth # term of geometric progression import math
def Nth_of_GP(a, r, N):
# Using formula to find the Nth
# term TN = a1 * r(N-1)
return (a * ( int )(math. pow (r, N - 1 )))
# Driver code if __name__ = = "__main__" :
a = 2 # Starting number
r = 3 # Common ratio
N = 5 # N th term to be find
# Function call
print ( "The" , N, "th term of the series is :" ,
Nth_of_GP(a, r, N))
# This code is contributed by Smitha Dinesh Semwal |
// C# program to find nth term // of geometric progression using System;
class GFG {
public static int Nth_of_GP( int a, int r, int N)
{
// using formula to find the Nth
// term TN = a1 * r(N-1)
return (a * ( int )(Math.Pow(r, N - 1)));
}
// Driver code
public static void Main()
{
// starting number
int a = 2;
// Common ratio
int r = 3;
// N th term to be find
int N = 5;
// Function call
Console.Write( "The " + N + "th term of the"
+ " series is : "
+ Nth_of_GP(a, r, N));
}
} // This code is contributed by vt_m |
// JavaScript Program to find nth term of // geometric progression function Nth_of_GP(a, r, N)
{ // using formula to find
// the Nth term
// TN = a1 * r(N-1)
return ( a * Math.floor(Math.pow(r, N - 1)) );
} // Driver code // starting number
let a = 2;
// Common ratio
let r = 3;
// N th term to be find
let N = 5;
// Display the output
document.write( "The " + N + "th term of the series is : "
+ Nth_of_GP(a, r, N));
// This code is contributed by Surbhi Tyagi |
<?php // PHP Program to find nth term of // geometric progression function Nth_of_GP( $a , $r , $N )
{ // using formula to find
// the Nth term TN = a1 * r(N-1)
return ( $a * (int)(pow( $r , $N - 1)) );
} // Driver code // starting number $a = 2;
// Common ratio $r = 3;
// N th term to be find $N = 5;
// Function call echo ( "The " . $N . "th term of the series is : "
. Nth_of_GP( $a , $r , $N ));
// This code is contributed by Ajit. ?> |
The 5th term of the series is : 162
Time complexity: O(log N) because using the inbuilt pow function
Auxiliary Space: O(1)
Approach 2(Using Loop): To solve the problem follow the below idea:
- Initialize a variable NthTerm to hold the Nth term of the geometric progression series, and set it equal to the first term of the series.
- Use a for loop to iterate over the first N-1 terms of the series, multiplying each term by the common ratio to get the next term.
- Print out the calculated Nth term of the series.
Below is the implementation of the above approach:
#include <iostream> using namespace std;
int Nth_of_GP( int a, int r, int N)
{ int NthTerm = a;
for ( int i = 1; i < N; i++) {
NthTerm *= r;
}
return NthTerm;
} int main()
{ // starting number
int a = 2;
// Common ratio
int r = 3;
// N th term to be find
int N = 5;
// Function call
cout << "The " << N << "th term of the series is : "
<< Nth_of_GP(a, r, N);
return 0;
} // This code is contributed by Taranpreet Singh. |
// java program to find nth term // of geometric progression import java.io.*;
import java.lang.*;
class GFG {
public static int Nth_of_GP( int a, int r, int N)
{
int NthTerm = a;
for ( int i = 1 ; i < N; i++) {
NthTerm *= r;
}
return NthTerm;
}
// Driver code
public static void main(String[] args)
{
// starting number
int a = 2 ;
// Common ratio
int r = 3 ;
// N th term to be find
int N = 5 ;
// Function call
System.out.print( "The " + N + "th term of the"
+ " series is : "
+ Nth_of_GP(a, r, N));
}
} |
# Nikunj Sonigara def Nth_of_GP(a, r, N):
NthTerm = a
for i in range ( 1 , N):
NthTerm * = r
return NthTerm
def main():
# Starting number
a = 2
# Common ratio
r = 3
# Nth term to be found
N = 5
# Function call
print (f "The {N}th term of the series is: {Nth_of_GP(a, r, N)}" )
if __name__ = = "__main__" :
main()
|
using System;
class MainClass
{ // Function to find the Nth term of a geometric progression (GP)
static int NthTermOfGP( int a, int r, int N)
{
int NthTerm = a;
// Calculate the Nth term using the common ratio
for ( int i = 1; i < N; i++)
{
NthTerm *= r;
}
return NthTerm;
}
public static void Main( string [] args)
{
// Starting number
int a = 2;
// Common ratio
int r = 3;
// Nth term to be found
int N = 5;
// Function call
Console.WriteLine( "The " + N + "th term of the series is : " + NthTermOfGP(a, r, N));
}
} |
// Nikunj Sonigara function Nth_of_GP(a, r, N) {
let NthTerm = a;
for (let i = 1; i < N; i++) {
NthTerm *= r;
}
return NthTerm;
} function main() {
// Starting number
const a = 2;
// Common ratio
const r = 3;
// Nth term to be found
const N = 5;
// Function call
console.log(`The ${N}th term of the series is: ${Nth_of_GP(a, r, N)}`);
} // Call the main function to start the program. main(); |
Output
The 5th term of the series is : 162
Time complexity: O(log N)
Auxiliary Space: O(1)