假定 十進位制整數 >= 0;
#include <stdio.h>
char * dto2(int x, char *s){
int y,i=0,L;
y=x;
while(1){
s[i]= y%2 + "0";
y=y/2;
i++;
if (y==0) break;
}
s[i]="\0";
L=strlen(s);
for (i=0;i<L/2; i++){y=s[i];s[i]=s[L-1-i];s[L-1-i]=y;}
return s;
int main()
{
int x;
char s[50];
printf("input positive int x:\n");
scanf("%d",&x);
if (x<0) {printf("x must be >=0\n"); return 1; };
dto2(x, s);
printf("%s",s );
return 0;
假定 十進位制整數 >= 0;
#include <stdio.h>
char * dto2(int x, char *s){
int y,i=0,L;
y=x;
while(1){
s[i]= y%2 + "0";
y=y/2;
i++;
if (y==0) break;
}
s[i]="\0";
L=strlen(s);
for (i=0;i<L/2; i++){y=s[i];s[i]=s[L-1-i];s[L-1-i]=y;}
return s;
}
int main()
{
int x;
char s[50];
printf("input positive int x:\n");
scanf("%d",&x);
if (x<0) {printf("x must be >=0\n"); return 1; };
dto2(x, s);
printf("%s",s );
return 0;
}