以下为C代码,在Win-tc下编译成功!
方法一:
int main()
{
int ival, n = 0;
char bin[32];
scanf(“%d”, &ival);
while(ival > 0) {
bin[n++] = ival % 2;
ival /= 2;
}
for(n–; n >= 0; n–)
printf(“%d”, bin[n]);
printf(“/n”);
getch();
}
方法二:
int main()
{
int ival, n = 0;
char bin[32];
scanf(“%d”, &ival);
while(ival > 0) {
bin[n++] = ival & 0x01;
ival >>= 1;
}
for(n–; n >= 0; n–)
printf(“%d”, bin[n]);
printf(“/n”);
getch();
}