![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 mth and nth terms using code, we need to simplify the formula. Let Sm be the sum of first m terms and Sn be the sum of the first n terms of the A.P.
a - first term,
d - common difference,
Given, Sm / Sn = m2 / n2
Formula for S, Sm = (m/2)[ 2*a + (m-1)*d ]
m2 / n2 = (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 mth term is,
Tm = a + (m-1)d
Ratio of mth and nth term is,
Tm / Tn = 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 mth and nth 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.