Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)

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 \mathbf{\frac{(bc - ad)} {(b - c)}}.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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)
?>

chevron_right



Output:

ΔX = 6


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.



Improved By : manishshaw1