- #1
Toyona10
- 31
- 0
Hi guys~
I'm currently learning C and we've been asked to write a code where a decimal-system number gets converted to its corresponding binary form BUT, you know, when we do it manually by the 2-division method how the order of the binary numbers go from down to up...? yeah that is the part we were told to ignore for now because it requires us to learn another lesson.
So, we were just told to give the output in the reverse form for now for example: 245 in binary is 11110101 but we were just told to give it in the reverse order for now like: 10101111...anyways, i kind of did it but for some reason it doesn't give me the last digit for any input: for 245 for instance, it only gives me 1010111 and excludes the last 1 there.
Here's my code:
I'd like to know why this is happening...when i walk through the code, everything seems fine but what's with the last digit being excluded?
I'm currently learning C and we've been asked to write a code where a decimal-system number gets converted to its corresponding binary form BUT, you know, when we do it manually by the 2-division method how the order of the binary numbers go from down to up...? yeah that is the part we were told to ignore for now because it requires us to learn another lesson.
So, we were just told to give the output in the reverse form for now for example: 245 in binary is 11110101 but we were just told to give it in the reverse order for now like: 10101111...anyways, i kind of did it but for some reason it doesn't give me the last digit for any input: for 245 for instance, it only gives me 1010111 and excludes the last 1 there.
Here's my code:
Code:
#include <stdio.h>
void main()
{
int a, mod;
scanf("%d",&a);
while(a/2!=0)
{
mod=a%2;
printf("%d",mod);
a=a/2;
}
}
I'd like to know why this is happening...when i walk through the code, everything seems fine but what's with the last digit being excluded?
Last edited by a moderator: