- 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

# C++ Ratio of Mth and Nth Terms of an A. P. with Given Ratio of Sums

Discuss a problem where we are given a ratio of sums of m and n terms of A.P. We need to find the ratio of mth and nth terms.

Input: m = 8, n = 4 Output: 2.142 Input: m = 3, n = 2 Output: 1.666 Input: m = 7, n = 3 Output: 2.6

## Approach to Find the Solution

To find the ratio of m^{th} and n^{th} terms using code, we need to simplify the formula. Let S_{m} be the sum of first m terms and S_{n} be the sum of the first n terms of the A.P.

a - first term,

d - common difference,

Given, **S _{m} / S_{n} = m^{2} / n^{2}**

Formula for S, S_{m} = (m/2)[ 2*a + (m-1)*d ]

m^{2} / n^{2} = (m/2)[ 2*a + (m-1)*d ] / (n/2)[ 2*a + (n-1)*d ]

m / n = [ 2*a +(m-1) *d ] / [ 2*a +(m-1) *d ]

Using cross multiplication,

n[ 2*a + (m−1)*d ] = m[ 2*a + (n−1)*d ]

2an + mnd - nd = 2am + mnd - md

2an - 2am = nd - md

(n - m)2a = (n-m)d

**d = 2a**

The formula of m^{th} term is,

T_{m} = a + (m-1)d

Ratio of m^{th} and n^{th} term is,

T_{m} / T_{n} = a + (m-1)d / a + (n-1)d

Replacing d with 2a,

Tm / Tn = a + (m-1)*2a / a + (n-1)*2a

Tm / Tn = a( 1 + 2m − 2 ) / a( 1 + 2n − 2 )

**Tm / Tn = 2m - 1 / 2n - 1**

So now we have a simple formula to find the ratio of m^{th} and n^{th} terms. Let’s see C++ code for this.

## Example

**C++ Code for the Above Approach**

#include <bits/stdc++.h> using namespace std; int main(){ float m = 8, n = 4; // calculating ratio by applying formula. float result = (2 * m - 1) / (2 * n - 1); cout << "The Ratio of mth and nth term is: " << result; return 0; }

## Output

The ratio of mth and nth term is: 2.14286

## Conclusion

In this tutorial, we discussed a problem to find the ratio of mth and nth term with a given ratio of sums which we solved by simplifying the formula of the sum of m terms and formula of mth term. We also discussed the C++ program for this problem which we can do with programming languages like C, Java, Python, etc. We hope you find this tutorial helpful.

- Related Questions & Answers
- Find Pth term of a GP if Mth and Nth terms are given in C++
- What are the pitfalls of the Price Earnings Ratio (P/E Ratio)?
- Transformation Ratio and Turn Ratio of Single-Phase Transformer
- Difference Between Acid Test Ratio and Current Ratio
- How to maintain the aspect ratio of an element with CSS?
- Price-Earnings Ratio vs. Earnings Yield Ratio
- Importance of Leverage Ratio Calculation in Finance
- What are the Types of Activity Ratio?
- What is the Importance of Profitability Ratio?
- What are the Limitations of Ratio Analysis?
- Significance of Short Circuit Ratio of Alternator (Synchronous Machine)
- Checking Compression ratio of a table in SAP HANA
- How to create a column with ratio of two columns in R?
- How to change the aspect ratio of an image in JavaFX?
- Cash Ratio: Definition and Analysis