Read a whole number n and print it in binary, without leading zeros.
Input
One number n (0 ≤ n ≤ 1,000,000,000).
Output
n in binary.
Examples
Example 1 · Input
13
Expected output
1101
Example 2 · Input
0
Expected output
0
Hints
Hint 1
Divide by 2 again and again; the remainders are the binary digits, last one first.
Hint 2
Store the digits in an array, then print it backwards.
Hint 3
0 is a special case: its binary form is 0.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
if (n == 0) {
printf("0\n");
return 0;
}
int bits[32];
int count = 0;
while (n > 0) {
bits[count++] = n % 2;
n /= 2;
}
for (int i = count - 1; i >= 0; i--) printf("%d", bits[i]);
printf("\n");
return 0;
}