Converting digit to binary using the POW function
Hello everyone,
I'm sorry I haven't had a chance to make a proper introduction yet, but I've been reading your forums for some time and I finally have a question that I need help on. Thanks in advance for any help :)
I'm currently on an assignment where I have to convert a digit to binary. This task is a little different than most conversions (to binary, at least) since we're forbidden from using strings or arrays (so the binary number that gets returned is techinically an integer, which is why we won't be asked to convert a binary number higher than 100). I also must make use of the pow function, using a base of 10.
I'll tell you what I have so far (I know it's horribly wrong, try not to laugh too hard :)):
Code:
void getBinary(int number){
int position=0;
int tmp=0;
while(number!=0){
tmp=number%2+number%2*pow(10,position);
number=number/2;
position=position+1;
}
printf(" %d", tmp);
}
The problem is that the number I'm getting is just wrong. I know the method about getting a binary number (taking the modulus of your number, dividing it by two, taking the modulus of that number, etc...), I just don't know how to translate it to C-speak.
It should also be mentioned that this isn't the entire code by the long shot. It's a really long assignment and this is only a part of it. This void function is called upon again at the end of the program, through use of another function. I've gotten the rest of the program to work besides this part, so any help would be greatly appreciated. Thanks!