Convert decimal number to binary in C -
for assignment need convert 16-bit decimal number binary number. example number 9 should print 0000000000001001. professor started code:
void printbinary(short n) { } int main(int argc, char **argv) { short n; printf("enter number: "); scanf("%hd", &n); printbinary(n); }
i confused go here. appreciate helping me understand do, new coding. in advance.
try this:
int printbinary(int integer) { int remainder, counter = 0, my_binary = 0; while (integer > 0) { remainder = integer % 2; my_binary += remainder * pow(10, counter); integer /= 2; counter++; } printf ("binary %d\n", my_integer"); }
this has been taken here.
note: have assumption want convert positive decimals.
Comments
Post a Comment