Related Articles
Reduce the fraction to its lowest form
• Difficulty Level : Basic
• Last Updated : 30 Apr, 2019

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ program to reduce a fraction x/y ``// to its lowest form`` ` `#include ``using` `namespace` `std;`` ` `// Function to reduce a fraction to its lowest form``void` `reduceFraction(``int` `x, ``int` `y)``{``    ``int` `d;``    ``d = __gcd(x, y);`` ` `    ``x = x / d;``    ``y = y / d;`` ` `    ``cout << ``"x = "` `<< x << ``", y = "` `<< y << endl;``}`` ` `// Driver Code``int` `main()``{``    ``int` `x = 16;``    ``int` `y = 10;`` ` `    ``reduceFraction(x, y);`` ` `    ``return` `0;``}`

## Java

 `// Java program to reduce a fraction x/y ``// to its lowest form ``class` `GFG ``{`` ` `// Function to reduce a fraction to its lowest form ``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); ``     ` `}`` ` `// Driver Code ``public` `static` `void` `main(String[] args)``{``    ``int` `x = ``16``; ``    ``int` `y = ``10``; `` ` `    ``reduceFraction(x, y);``}``}`` ` `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 program to reduce a fraction x/y ``# to its lowest form``from` `math ``import` `gcd `` ` `# Function to reduce a fraction ``# to its lowest form``def` `reduceFraction(x, y) :``     ` `    ``d ``=` `gcd(x, y);`` ` `    ``x ``=` `x ``/``/` `d;``    ``y ``=` `y ``/``/` `d;`` ` `    ``print``(``"x ="``, x, ``", y ="``, y);`` ` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:`` ` `    ``x ``=` `16``;``    ``y ``=` `10``;`` ` `    ``reduceFraction(x, y);`` ` `# This code is contributed by Ryuga`

## C#

 `// C# program to reduce a fraction x/y ``// to its lowest form ``using` `System; `` ` `class` `GFG ``{``  ` `// Function to reduce a fraction to its lowest form ``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); ``      ` `}``  ` `// Driver Code ``public` `static` `void` `Main(String[] args)``{``    ``int` `x = 16; ``    ``int` `y = 10; ``  ` `    ``reduceFraction(x, y);``}``}`` ` `// This code has been contributed by 29AjayKumar`

## PHP

 ``

Output:
```x = 8, y = 5
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up