Her is the code...u need to add the if block for -ve numbers.

1 public static String printBinary(String n) {
2 int intPart = Integer.parseInt(n.substring(0, n.indexOf(‘.’)));
3 double decPart = Double.parseDouble(
4 n.substring(n.indexOf(‘.’), n.length()));
5 String int_string = “”;
6 while (intPart > 0) {
7 int r = intPart % 2;
8 intPart >>= 1;
9 int_string = r + int_string;
10 }
11 StringBuffer dec_string = new StringBuffer();
12 while (decPart > 0) {
13 if (dec_string.length() > 32) return “ERROR”;
14 if (decPart == 1) {
15 dec_string.append((int)decPart);
16 break;
17 }
18 double r = decPart * 2;
19 if (r >= 1) {
20 dec_string.append(1);
21 decPart = r - 1;
22 } else {
23 dec_string.append(0);
24 decPart = r;
25 }
26 }
27 return int_string + “.” + dec_string.toString();
28 }

-----Original Message-----
From: algogeeks@googlegroups.com [mailto:algoge...@googlegroups.com] On
Behalf Of snehal jain
Sent: Saturday, January 08, 2011 2:40 PM
To: Algorithm Geeks
Subject: [algogeeks] binary

Given a (decimal - e.g.3.72) number that is passed in as a string,
print the binary rep¬resentation.If the number can not be represented
accurately in binary, print “ERROR

-- 
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to