Skip to content
Related Articles

Related Articles

Improve Article

Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given

  • Last Updated : 19 Mar, 2021

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:
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 / d
or ((a * S) + x * (a + b)) / ((b * S) + x * (a + b)) = c / d
So, 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;
}

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

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

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

Javascript




<script>
 
// javascript implementation of the above 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
function sum(a , b, c , d, x)
{
    var ans = (x * (a + b) * (c - d)) /
                     ((a * d) - (b * c));
    return ans;
}
 
// Driver code
 
var a = 1, b = 2, c = 9, d = 13, x = 5;
 
document.write(sum(a, b, c, d, x));
 
 
// This code is contributed by 29AjayKumar
 
</script>
Output: 
12

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :