I have an assignment to write a function that prints out a binary version of an entered number. This is the code I have so far:
Code:
#include <stdio.h>
void printBinary(short n)
{
int r;
short result, i;
result=0;
for (i=1; n!=0; i=i*10);
{
r=n%2;
result=result+(r*i);
n=n/2;
}
printf("Binary: %d ", result);
}
int main(int argc, char **argv)
{
short n;
printf("Enter number: ");
scanf("%hd", &n);
printBinary(n);
}
For some reason the program works only when I enter 0. In all other cases it does nothing. Doesn't even give an error message, neither does the text "Binary: " appear. Any help is appreciated.