Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Test {
- static int gcd(int a, int b) {
- int r; // Euclid's algorithm
- do {
- r = a % b;// find remainder
- a = b; // swap : smaller to bigger
- b = r; // remainder to smaller
- } while (r != 0);
- return a;
- }
- public static void main(String args[]) {
- double d = 2.305;
- int denom=1;
- while (Math.abs(d-(int)d)>1e-8) // d notwhole number
- {
- d*=10;
- denom*=10;
- }
- int num=(int)d;
- int f= gcd(num, denom);
- System.out.println(num/f+ " / "+ denom/f); // 461/200
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment