十进制数转换成二进制数中1的个数 循环实现
时间:2010-05-31 来源:随1意2o
#include <stdio.h>
main()
{
int n,w,sum = 0; printf("please input a number(Ê®½øÖÆ):");
scanf("%d",&n);
w = n;
for(;n;n = n/2)
{
sum += n%2;
}
printf("%d\n",sum); for(; w ; w = w / 2)
printf("%d",w%2); // 输出输入的十进制的二进制的倒序数,以便观察运行结果的正确性
printf("\n");
}
main()
{
int n,w,sum = 0; printf("please input a number(Ê®½øÖÆ):");
scanf("%d",&n);
w = n;
for(;n;n = n/2)
{
sum += n%2;
}
printf("%d\n",sum); for(; w ; w = w / 2)
printf("%d",w%2); // 输出输入的十进制的二进制的倒序数,以便观察运行结果的正确性
printf("\n");
}
相关阅读 更多 +