Given that the ratio to sum of first m and n terms of an A.P. with first term ‘a’ and commond difference ‘d’ is **m^2:n^2**. The task is to find the ratio of mth and nth term of this A.P.

Examples:

Input: m = 3, n = 2 Output: 1.6667 Input: m = 5, n = 3 Output: 1.8

**Approach:**

Let the Sum of first m and n terms be denoted by Sm and Sn respectively.

Also, let the mth and nth term be denoted by tm and tn respectively.

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

Sn = (n * [ 2*a + (n-1)*d ])/2Given: Sm / Sn = m^2 / n^2

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

=> (2*a + (m-1)*d) / (2*a + (n-1)*d) = m / non cross multiplying and solving, we get

d = 2 * aHence, the mth and nth terms can be written as:

mth term = tm = a +(m-1)*d = a + (m-1)*(2*a)

nth term = tn = a +(n-1)*d = a + (n-1)*(2*a)Hence the ratio will be:

tm / tn = (a + (m-1)*(2*a)) / (a + (n-1)*(2*a))

tm / tn =(2*m – 1) / (2*n – 1)

Below is the required implementation:

`// C++ code to calculate ratio ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// function to calculate ratio of mth and nth term ` `float` `CalculateRatio(` `float` `m, ` `float` `n) `
`{ ` ` ` `// ratio will be tm/tn = (2*m - 1)/(2*n - 1) `
` ` `return` `(2 * m - 1) / (2 * n - 1); `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `float` `m = 6, n = 2; `
` ` `cout << CalculateRatio(m, n); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java code to calculate ratio ` `import` `java.io.*; `
` ` `class` `Nth { `
` ` `// function to calculate ratio of mth and nth term ` `static` `float` `CalculateRatio(` `float` `m, ` `float` `n) `
`{ ` ` ` `// ratio will be tm/tn = (2*m - 1)/(2*n - 1) `
` ` `return` `(` `2` `* m - ` `1` `) / (` `2` `* n - ` `1` `); `
`} ` `} ` ` ` `// Driver code ` `class` `GFG { `
` ` ` ` `public` `static` `void` `main (String[] args) { `
` ` `float` `m = ` `6` `, n = ` `2` `; `
` ` `Nth a=` `new` `Nth(); `
`System.out.println(a.CalculateRatio(m, n)); ` ` ` ` ` `} `
`} ` ` ` `// this code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

`# Python3 program to calculate ratio ` ` ` `# function to calculate ratio ` `# of mth and nth term ` `def` `CalculateRatio(m, n): `
` ` ` ` `# ratio will be tm/tn = (2*m - 1)/(2*n - 1) `
` ` `return` `(` `2` `*` `m ` `-` `1` `) ` `/` `(` `2` `*` `n ` `-` `1` `); `
` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: `
` ` `m ` `=` `6` `; `
` ` `n ` `=` `2` `; `
` ` `print` `(` `float` `(CalculateRatio(m, n))); `
` ` `# This code is contributed by ` `# Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

`// C# code to calculate ratio ` `using` `System; `
` ` `class` `Nth { `
` ` `// function to calculate ratio of mth and nth term ` `float` `CalculateRatio(` `float` `m, ` `float` `n) `
`{ ` ` ` `// ratio will be tm/tn = (2*m - 1)/(2*n - 1) `
` ` `return` `(2 * m - 1) / (2 * n - 1); `
`} ` ` ` ` ` `// Driver code `
` ` `public` `static` `void` `Main () { `
` ` `float` `m = 6, n = 2; `
` ` `Nth a=` `new` `Nth(); `
`Console.WriteLine(a.CalculateRatio(m, n)); ` ` ` ` ` `} `
`} ` `// this code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

`<?php ` `// PHP code to calculate ratio ` ` ` `// function to calculate ratio ` `// of mth and nth term ` `function` `CalculateRatio( ` `$m` `, ` `$n` `) `
`{ ` ` ` `// ratio will be tm/tn = (2*m - 1)/(2*n - 1) `
` ` `return` `(2 * ` `$m` `- 1) / (2 * ` `$n` `- 1); `
`} ` ` ` `// Driver code ` ` ` `$m` `= 6; ` `$n` `= 2; `
`echo` `CalculateRatio(` `$m` `, ` `$n` `); `
` ` `// This code is contributed ` `// by inder_verma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3.66667

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of P terms of an AP if Mth and Nth terms are given
- Find Pth term of a GP if Mth and Nth terms are given
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Mth bit in Nth binary string from a sequence generated by the given operations
- Find n terms of Fibonacci type series with given first two terms
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Find the Mth element of the Array after K left rotations
- Mth element after K Right Rotations of an Array
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Find nth Fibonacci number using Golden ratio
- Count sequences of given length having non-negative prefix sums that can be generated by given values
- First N natural can be divided into two sets with given difference and co-prime sums
- Program to convert given number of days in terms of Years, Weeks and Days
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Generate a combination of minimum coins that sums to a given value

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

**Practice Tags :**