Algorithm
problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1755
You are to write a program that reduces a fraction into its lowest terms. Input The first line of the input file gives the number of test cases N (≤ 20). Each of the following N lines contains a fraction in the form of p/q (1 ≤ p, q ≤ 1030). Output For each test case, output the fraction after simplification.
Sample Input 4 1 / 2 2 / 4 3 / 3 4 / 2
Sample Output 1 / 2 1 / 2 1 / 1 2 / 1
Code Examples
#1 Code Example with C Programming
Code -
C Programming
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t= sc.nextInt();
while(t-- > 0){
BigInteger numerator = sc.nextBigInteger();
sc.next();
BigInteger denominator = sc.nextBigInteger();
BigInteger gcd = numerator.gcd(denominator);
numerator = numerator.divide(gcd);
denominator = denominator.divide(gcd);
System.out.println(numerator + " / " + denominator);
}
}
}
Copy The Code &
Try With Live Editor
Input
1 / 2
2 / 4
3 / 3
4 / 2
Output
1 / 2
1 / 1
2 / 1
Demonstration
UVA Online Judge solution - 10814-Simplifying Fractions - UVA Online Judge solution in C,C++,java