Given a ratio **a : b** of two unknown numbers. When both the numbers are incremented by a given integer **x**, the ratio becomes **c : d**. The task is to find the sum of the two numbers.

**Examples:**

Input:a = 2, b = 3, c = 8, d = 9, x = 6

Output:5

Original numbers are 2 and 3

Original ratio = 2:3

After adding 6, ratio becomes 8:9

2 + 3 = 5

Input:a = 1, b = 2, c = 9, d = 13, x = 5

Output:12

**Approach:** Let the sum of the numbers be **S**. Then, the numbers can be **(a * S)/(a + b)** and **(b * S)/(a + b)**.

Now, as given:

(((a * S) / (a + b)) + x) / (((b * S) / (a + b)) + x) = c / dor((a * S) + x * (a + b)) / ((b * S) + x * (a + b)) = c / dSo,S = (x * (a + b) * (c - d)) / (ad - bc)

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the sum of numbers ` `// which are in the ration a:b and after ` `// adding x to both the numbers ` `// the new ratio becomes c:d ` `double` `sum(` `double` `a, ` `double` `b, ` `double` `c, ` `double` `d, ` `double` `x) ` `{ ` ` ` `double` `ans = (x * (a + b) * (c - d)) / ((a * d) - (b * c)); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `double` `a = 1, b = 2, c = 9, d = 13, x = 5; ` ` ` ` ` `cout << sum(a, b, c, d, x); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the sum of numbers ` ` ` `// which are in the ration a:b and after ` ` ` `// adding x to both the numbers ` ` ` `// the new ratio becomes c:d ` ` ` `static` `double` `sum(` `double` `a, ` `double` `b, ` ` ` `double` `c, ` `double` `d, ` ` ` `double` `x) ` ` ` `{ ` ` ` `double` `ans = (x * (a + b) * (c - d)) / ` ` ` `((a * d) - (b * c)); ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `double` `a = ` `1` `, b = ` `2` `, c = ` `9` `, d = ` `13` `, x = ` `5` `; ` ` ` ` ` `System.out.println(sum(a, b, c, d, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the sum of numbers ` `# which are in the ration a:b and after ` `# adding x to both the numbers ` `# the new ratio becomes c:d ` `def` `sum` `(a, b, c, d, x): ` ` ` ` ` `ans ` `=` `((x ` `*` `(a ` `+` `b) ` `*` `(c ` `-` `d)) ` `/` ` ` `((a ` `*` `d) ` `-` `(b ` `*` `c))); ` ` ` `return` `ans; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `a, b, c, d, x ` `=` `1` `, ` `2` `, ` `9` `, ` `13` `, ` `5` `; ` ` ` ` ` `print` `(` `sum` `(a, b, c, d, x)); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the sum of numbers ` ` ` `// which are in the ration a:b and after ` ` ` `// adding x to both the numbers ` ` ` `// the new ratio becomes c:d ` ` ` `static` `double` `sum(` `double` `a, ` `double` `b, ` ` ` `double` `c, ` `double` `d, ` ` ` `double` `x) ` ` ` `{ ` ` ` `double` `ans = (x * (a + b) * (c - d)) / ` ` ` `((a * d) - (b * c)); ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `double` `a = 1, b = 2, ` ` ` `c = 9, d = 13, x = 5; ` ` ` ` ` `Console.WriteLine(sum(a, b, c, d, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

12

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:

- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Create new linked list from two given linked list with greater element at each node
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Program to find the count of coins of each type from the given ratio
- Generate original array from difference between every two consecutive elements
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Check whether two numbers are in silver ratio
- Check whether two numbers are in golden ratio
- Count of carry operations on adding two Binary numbers
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Count distinct elements after adding each element of First Array with Second Array
- Find the original matrix when largest element in a row and a column are given
- Minimum sum obtained from groups of four elements from the given array
- Check if sum Y can be obtained from the Array by the given operations
- Probability of obtaining Prime Numbers as product of values obtained by throwing N dices

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.