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

x
+
cmd
4
1 / 2
2 / 4
3 / 3
4 / 2

Output

x
+
cmd
1 / 2
1 / 2
1 / 1
2 / 1
Advertisements

Demonstration


UVA Online Judge solution - 10814-Simplifying Fractions - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution -10812-Beat the Spread! - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10815-Andy's First Dictionary - UVA Online Judge solution in C,C++,java