Skip to content
Related Articles

Related Articles

Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
  • Last Updated : 07 Apr, 2021

Given a fraction in form of a/b where a & b are positive integers. Find ΔX such that when it is added to numerator as well as denominator of given fraction it will result into a new Ir-reducible fraction c/d. 
Examples: 
 

Input : a = 4, b = 10, c = 1, d = 2
Output : ΔX = 2
Explanation : (a + ΔX)/(b + ΔX) =
 (4 + 2) / (10 + 2) = 6/12 = 1/2 = c/d

Input : a = 4, b = 10, c = 2, d = 5
Output : ΔX = 0
Explanation : (a + ΔX) / (b + ΔX) = 
(4) / (10) = 2 / 5 = c/d

 

To solve this type of problem rather than implementing just a programming approach we have to do a little of mathematics. mathematics just necessary is as: 
 

As per question we have to find ΔX such that:
=> (a + ΔX) / (b + ΔX) = c / d
=> ad + dΔX = bc + cΔX
=> dΔX - cΔX = bc - ad
=> ΔX (d - c) = bc -ad
=> ΔX = (bc - ad) / (d - c)

So, in a way to finding ΔX we have to only calculate 

*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty


 



C++




// C++ Program to find deltaX
#include <bits/stdc++.h>
using namespace std;
 
// function to find delta X
int findDelta(int a, int b, int c, int d)
{
    return (b * c - a * d) / (d - c);
}
 
// driver program
int main()
{
    int a = 3, b = 9, c = 3, d = 5;
 
    // u0394X is code for delta sign
    cout << "\u0394X = " << findDelta(a, b, c, d);
    return 0;
}

Java




// Java Program to
// find deltaX
import java.io.*;
 
class GFG
{
    // function to find delta X
    static int findDelta(int a, int b,
                         int c, int d)
    {
        return (b * c - a *
                d) / (d - c);
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int a = 3, b = 9,
            c = 3, d = 5;
     
        // u0394X is code
        // for delta sign
        System.out.print("\u0394X = " +
                findDelta(a, b, c, d));
    }
}
 
// This code is contributed
// by Manish Shaw(manishshaw1)

Python3




# Python Program to find deltaX
# !/usr/bin/python
# coding=utf-8
 
# function to find delta X
def findDelta(a, b, c, d) :
 
    return int((b * c -
                a * d) /
               (d - c));
 
# Driver Code
a = 3; b = 9;
c = 3; d = 5;
 
# u0394X is code
# for delta sign
print ("X = {}" .
        format(findDelta(a, b,
                         c, d)));
 
# This code is contributed by
# Manish Shaw(manishshaw1)

C#




// C# Program to
// find deltaX
using System;
 
class GFG
{
// function to find delta X
static int findDelta(int a, int b,
                     int c, int d)
{
    return (b * c - a *
            d) / (d - c);
}
 
// Driver Code
static void Main()
{
    int a = 3, b = 9,
        c = 3, d = 5;
 
    // u0394X is code
    // for delta sign
    Console.Write("\u0394X = " +
                  findDelta(a, b, c, d));
}
}
 
// This code is contributed
// by Manish Shaw(manishshaw1)

PHP




<?php
// PHP Program to find deltaX
 
// function to find delta X
function findDelta($a, $b,
                   $c, $d)
{
    return ($b * $c -
            $a * $d) / ($d - $c);
}
 
// Driver Code
$a = 3; $b = 9;
$c = 3; $d = 5;
 
// u0394X is code for delta sign
echo "?X = ".findDelta($a, $b,
                       $c, $d);
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>

Javascript




<script>
// JavaScript Program to
// find deltaX
 
 // function to find delta X
    function findDelta(a, b, c, d)
    {
        return (b * c - a *
                d) / (d - c);
    }
   
// Driver Code
        let a = 3, b = 9,
            c = 3, d = 5;
       
        // u0394X is code
        // for delta sign
        document.write("\u0394X = " +
                findDelta(a, b, c, d));
 
// This code is contributed by splevel62.
</script>

Output: 
 

ΔX = 6

 

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 :