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.
Algorithm:
- Create the “reduceFraction” function, which has the two integer inputs x and y.
- Declare the variable d as an integer.
- Call the __gcd() method with the inputs x and y, and then save the outcome in d.
- Divide x by d, then put the outcome back into x.
- Divide y by d, then add the answer back into y.
- Print the lowered fraction together with the revised x and y values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void reduceFraction( int x, int y)
{
int d;
d = __gcd(x, y);
x = x / d;
y = y / d;
cout << "x = " << x << ", y = " << y << endl;
}
int main()
{
int x = 16;
int y = 10;
reduceFraction(x, y);
return 0;
}
|
Java
class GFG
{
static 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);
}
public static void main(String[] args)
{
int x = 16 ;
int y = 10 ;
reduceFraction(x, y);
}
}
|
Python3
from math import gcd
def reduceFraction(x, y) :
d = gcd(x, y);
x = x / / d;
y = y / / d;
print ( "x =" , x, ", y =" , y);
if __name__ = = "__main__" :
x = 16 ;
y = 10 ;
reduceFraction(x, y);
|
C#
using System;
class GFG
{
static 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);
}
public static void Main(String[] args)
{
int x = 16;
int y = 10;
reduceFraction(x, y);
}
}
|
PHP
<?php
function 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 );
}
$x = 16;
$y = 10;
reduceFraction( $x , $y );
?>
|
Javascript
<script>
function 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);
}
let x = 16;
let y = 10;
reduceFraction(x, y);
</script>
|
Time Complexity: O(log(max(x,y)))
Auxiliary Space: O(log(max(x,y)))
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Mar, 2023
Like Article
Save Article