#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float n ,a, sum,count,count1;
printf("\nInput integer in base 10 : ");
scanf("%f", &n);
for(count=0 ; count<=100 ; ++count){
if(n/pow(2,count)<2 && n/pow(2,count)>=1)break;
}
a=n-pow(2,count);
sum=pow(10,count);
for(count1=count-1 ; count1>=0 ; --count){
if(a/pow(2,count1)<2 && a/pow(2,count1)>=1 ){
a-=pow(2,count1);
sum+=pow(10,count1);
}
}
printf("The binary of %.0f is %.0f",n,sum);
return 0;
}
此代码用于在不使用数组的情况下打印十进制数的二进制等价物
最佳答案
正如 @kiner_shah 在对您的问题的评论中所述,您在第二个循环中减少了一个不正确的变量。
for(count1=count-1 ; count1>=0 ; --count){
...
应该是
for(count1=count-1 ; count1>=0 ; --count1){
...
使用更清晰/描述性的变量名称来避免单字符错误是一个好习惯。
关于c - 我是 C 新手。第二个 for 循环永远不会结束为什么会这样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847500/