# Find the number which when added to the given ratio a : b, the ratio changes to c : d

Given four integers **a**, **b**, **c** and **d**. The task is to find the number **X** which when added to the numbers **a** and **b** the ratio changes from **a : b** to **c : d**.

**Examples:**

Input:a = 3, b = 6, c = 3, d = 4

Output:6

When 6 is added to a and b

a = 3 + 6 = 9

b = 6 + 6 = 12

And, the new ratio will be 9 : 12 = 3 : 4

Input:a = 2, b = 3, c = 4, d = 5

Output:2

**Approach:** Old ratio is **a : b** and new ratio is **c : d**. Let the required number be **X**,

So, **(a + X) / (b + X) = c / d**

or, ad + dx = bc + cx

or, x(d – c) = bc – ad

So, **x = (bc – ad) / (d – c)**

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 ` `// required number X ` `int` `getX(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `int` `X = (b * c - a * d) / (d - c); ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 2, b = 3, c = 4, d = 5; ` ` ` ` ` `cout << getX(a, b, c, d); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// required number X ` `static` `int` `getX(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `int` `X = (b * c - a * d) / (d - c); ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `a = ` `2` `, b = ` `3` `, c = ` `4` `, d = ` `5` `; ` ` ` `System.out.println (getX(a, b, c, d)); ` ` ` `} ` `} ` ` ` `// The code is contributed by ajit..@23 ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# required number X ` `def` `getX(a, b, c, d): ` ` ` ` ` `X ` `=` `(b ` `*` `c ` `-` `a ` `*` `d) ` `/` `/` `(d ` `-` `c) ` ` ` `return` `X ` ` ` `# Driver code ` ` ` `a ` `=` `2` `b ` `=` `3` `c ` `=` `4` `d ` `=` `5` ` ` `print` `(getX(a, b, c, d)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// required number X ` `static` `int` `getX(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `int` `X = (b * c - a * d) / (d - c); ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `a = 2, b = 3, c = 4, d = 5; ` ` ` `Console.Write(getX(a, b, c, d)); ` `} ` `} ` ` ` `// The code is contributed by Tushil. ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find amount to be added to achieve target ratio in a given mixture
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Find nth Fibonacci number using Golden ratio
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Find the ratio of number of elements in two Arrays from their individual and combined average
- Find the ratio of LCM to GCD of a given Array
- Find pair with maximum ratio in an Array
- Find if it is possible to get a ratio from given ranges of costs and quantities
- Program to find the common ratio of three numbers
- Program to find the count of coins of each type from the given ratio
- Check whether two numbers are in silver ratio
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Check whether two numbers are in golden ratio
- Program to calculate the profit sharing ratio
- Max count of unique ratio/fraction pairs in given arrays
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Section formula (Point that divides a line in given ratio)
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Find the minimum number to be added to N to make it a prime number
- Find the minimum number to be added to N to make it a power of K

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.