waltermilner

Untitled

May 16th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. class Test {
  2.  
  3.   static int gcd(int a, int b) {
  4.     int r; // Euclid's algorithm
  5.     do {
  6.       r = a % b;// find remainder
  7.       a = b; // swap : smaller to bigger
  8.       b = r; // remainder to smaller
  9.  
  10.     } while (r != 0);
  11.     return a;
  12.   }
  13.  
  14.   public static void main(String args[]) {
  15.     double d = 2.305;
  16.     int denom=1;
  17.     while (Math.abs(d-(int)d)>1e-8) // d notwhole number
  18.     {
  19.       d*=10;
  20.       denom*=10;
  21.     }
  22.     int num=(int)d;
  23.     int f= gcd(num, denom);
  24.     System.out.println(num/f+ " / "+ denom/f); // 461/200
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment