Ninja and his friends go to the park every day. But now, due to his homework, he wasn’t able to go to the park with his friends. So when he figures out, he notes that a lot of time is consumed in doing maths homework where he has to convert some real number to a binary string. So he is deciding to build a calculator which can convert real numbers to the binary string.
So help our Ninja to write a code for the calculator, which can convert the real numbers into a binary string and save his time to join his friends in the park.
So your task is to write a code that can convert real numbers between ‘0’ and ‘1’into a binary string.
For example, you have given a number “0.750” so you have to convert it in its binary form that is “0.110”
Input Format :
The first line of input contains the ‘T’ number of test cases.
The first line of each test case contains a real number ‘N’ between 0 and 1 (e.g., 0.72) as a double.
Output Format :
For each test case, print the binary representation in form of a string in case the number cannot be represented accurately in binary with at most 32 characters, return string as “ERROR”.
Note :
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100000
0 <= N < 1
Time Limit: 1 sec
2
0.625
0.72
0.101
ERROR
Test Case 1:
In the first line, there is the number of test cases i.e., 1, and in the next line ‘0.625’ is the real number in a decimal form which we have to convert into a binary string.
Here, we have started with multiplying the number that is ‘N’with ‘2’ and hence write its binary number like
String s= ‘0’+ ‘.’+ ‘1’+ ‘0’+ ‘1’ =[0.101].
Test case 2:
Here the number is “0.72” on multiplication with 2 we see we didn’t get its equivalent Binary number or we can say that we weren’t able to convert our number into binary form up to the 32 characters as 32 can be the maximum length of our string so we return our string as “ERROR”.
2
0.248
0.50
ERROR
0.1
Try to convert all digits in the form of 0 and 1
We multiply our number until it is less than 1 or the length is smaller than 32 bits.
Repeat until our number is greater than 0 and in case of string length exceeding
‘32’
Characters return string as “ERROR”.
O(1).
At maximum, our loop can run for 32 times the maximum length of string which is constant.
O(1).
we are using constant space.