- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find Pth term of a GP if Mth and Nth terms are given in C++

In this problem we are given five values m, n, mth term, nth term, p. Our task is to Find Pth term of a GP if Mth and Nth terms are given.

For a GP, we are given the values of mth term and nth term. Using these values, we need to find the Pth term of the series.

**Let’s take an example to understand the problem,**

## Input

m = 7, mthTerm = 1458, n = 10, nthterm = 39366, p = 3

## Output

18

## Solution Approach

Here, we are given a GP. let's suppose the GP is,

GP = a , a*r , a*(r^{2}), a*(r^{3}) ….

The formula for T^{th} term is

T^{th}Term = a * r^{(T-1)}

Now, we are given nth and mth term,

mth term = a * (r ^ (m-1)) nth term = a * (r ^ (n-1))

Dividing the equations we get,

mth term / nth term = (r ^(m - n))

Using this equation, we can find the value of r, Then a can be found using the value of mth term as

mth term = a * (r^(m-1))

Then after finding the value of a and r. The value of pth term can be easily found using,

pth term = a * (r^(p-1))

**Program to illustrate the working of our solution,**

## Example

#include <cmath> #include <iostream> using namespace std; double findRinGP(double m, double n, double mth, double nth) { if (m < n) return pow(nth / mth, 1.0 / (n - m)); return pow(mth / nth, 1.0 / (m - n)); } double findTermGP(int m, int n, double mth, double nth, int p) { double r = findRinGP(m, n, mth, nth); double a = mth / pow(r, (m - 1)); return ( a * pow(r, (p - 1)) ); } int main() { int m = 7, n = 10, p = 5; double mth = 1458, nth = 39366; cout<<"The "<<p<<"th of the series is "<<findTermGP(m, n, mth, nth, p); return 0; }

## Output

The 5th of the series is 162

- Related Questions & Answers
- C++ Ratio of Mth and Nth Terms of an A. P. with Given Ratio of Sums
- Find nth term of a given recurrence relation in Python
- Find nth term of a given recurrence relation in C++
- Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++
- C program to find nth term of given recurrence relation
- Find Nth term (A matrix exponentiation example) in C++
- Find nth term of the Dragon Curve Sequence in C++
- Program to find nth Fibonacci term in Python
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- JavaScript code to find nth term of a series - Arithmetic Progression (AP)
- Program to find nth term in Look and Say Sequence in Python
- Program to find Nth term divisible by a or b in C++
- Find the Nth term of the series 9, 45, 243,1377…in C++
- C++ program to find nth term of the series 5, 2, 13 41,...
- Find the nth term of the series 0, 8, 64, 216, 512,... in C++

Advertisements