Binary representation of a given number | GeeksforGeeks
Write a program to print Binary representation of a given number.
Write a program to print Binary representation of a given number.
Method 2: Recursive
step 1) if NUM > 1 a) push NUM on stack b) recursively call function with 'NUM / 2' step 2) a) pop NUM from stack, divide it by 2 and print it's remainder.
void bin(unsigned n) { if (n > 1) bin(n/2); // n>1, n%2 => n&1 printf ( "%d" , n % 2); } |
void
bin(unsigned n)
{
unsigned i;
for
(i = 1 << 31; i > 0; i = i / 2)
(n & i)?
printf
(
"1"
):
printf
(
"0"
);
}
Also check: http://massivealgorithms.blogspot.com/2015/06/lintcode-binary-representation.html
Given a (decimal - e g 3.72) number that is passed in as a string,return the binary representation that is passed in as a string.If the number can not be represented accurately in binary, print "ERROR" Example n = 3.72, return ERROR n = 3.5, return 11.1Read full article from Binary representation of a given number | GeeksforGeeks