Created
August 2, 2014 22:48
-
-
Save hilda8519/7aeb3efcbc60786edc6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Q52 { | |
public static void main(String[] args){ | |
System.out.println(printBinary(0.111111)); | |
System.out.println(printBinary(0.8)); | |
} | |
static String printBinary(double num){ | |
if(num>1 || num<0) | |
return "ERROR"; | |
double remain = num; | |
StringBuffer buffer = new StringBuffer(); | |
buffer.append("."); | |
while(remain>0){ | |
if(buffer.length()>32){ | |
return "ERROR"; | |
} | |
if(remain >= 0.5){ | |
buffer.append(1); | |
remain = remain-0.5; | |
} | |
else | |
buffer.append(0); | |
remain=remain*2; | |
} | |
return buffer.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment