Reduce the fraction to its lowest form

18 Mar, 2022
Given two integers x and y and where x is divisible by y. It can be represented in the form of a fraction x/y. The task is to reduce the fraction to its lowest form.Examples:  
Input : x = 16, y = 10
Output : x = 8, y = 5

Input : x = 10, y = 8
Output : x = 5, y = 4
 
Approach: Both of the values x and y will be divisible by their greatest common divisor. So if we divide x and y from the gcd(x, y) then x and y can be reduced to its simplest form.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to reduce a fraction x/y// to its lowest form #include <bits/stdc++.h>using namespace std; // Function to reduce a fraction to its lowest formvoid reduceFraction(int x, int y){    int d;    d = __gcd(x, y);     x = x / d;    y = y / d;     cout << "x = " << x << ", y = " << y << endl;} // Driver Codeint main(){    int x = 16;    int y = 10;     reduceFraction(x, y);     return 0;}
// Java program to reduce a fraction x/y// to its lowest formclass GFG{ // Function to reduce a fraction to its lowest formstatic void reduceFraction(int x, int y){    int d;    d = __gcd(x, y);     x = x / d;    y = y / d;     System.out.println("x = " + x + ", y = " + y);} static int __gcd(int a, int b){    if (b == 0)        return a;    return __gcd(b, a % b);     } // Driver Codepublic static void main(String[] args){    int x = 16;    int y = 10;     reduceFraction(x, y);}} /* This code contributed by PrinciRaj1992 */
# Python3 program to reduce a fraction x/y# to its lowest formfrom math import gcd # Function to reduce a fraction# to its lowest formdef reduceFraction(x, y) :         d = gcd(x, y);     x = x // d;    y = y // d;     print("x =", x, ", y =", y); # Driver Codeif __name__ == "__main__" :     x = 16;    y = 10;     reduceFraction(x, y); # This code is contributed by Ryuga
// C# program to reduce a fraction x/y// to its lowest formusing System; class GFG{  // Function to reduce a fraction to its lowest formstatic void reduceFraction(int x, int y){    int d;    d = __gcd(x, y);      x = x / d;    y = y / d;      Console.WriteLine("x = " + x + ", y = " + y);}  static int __gcd(int a, int b){    if (b == 0)        return a;    return __gcd(b, a % b);      }  // Driver Codepublic static void Main(String[] args){    int x = 16;    int y = 10;      reduceFraction(x, y);}} // This code has been contributed by 29AjayKumar
<?php// PHP program to reduce a fraction x/y// to its lowest form // Function to reduce a fraction to its lowest formfunction reduceFraction($x, $y){    $d;    $d = __gcd($x, $y);     $x = $x / $d;    $y = $y / $d;     echo("x = " . $x . ", y = " . $y);} function __gcd($a, $b){    if ($b == 0)        return $a;    return __gcd($b, $a % $b);     } // Driver Code$x = 16;$y = 10; reduceFraction($x, $y); // This code is contributed by Rajput-Ji?>
<script> // Javascript program to reduce a fraction x/y// to its lowest form // Function to reduce a fraction to its lowest formfunction reduceFraction(x, y){    let d;    d = __gcd(x, y);     x = parseInt(x / d);    y = parseInt(y / d);     document.write("x = " + x + ", y = " + y);} function __gcd(a, b){    if (b == 0)        return a;    return __gcd(b, a % b);      } // Driver Code    let x = 16;    let y = 10;     reduceFraction(x, y); </script>
Output:  
x = 8, y = 5
Time Complexity: O(log(max(x,y)))
Auxiliary Space: O(log(max(x,y))) 
ankthon
princiraj1992
29AjayKumar
Rajput-Ji
rishavmahato348
surindertarika1234
prophet1999
GCD-LCM
school-programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.