このプログラムを使用して10進数を2進数に変換しようとしていますが、出力には常に最後の桁がありません。
たとえば、商に「123」を入力すると、結果は「1111011」ではなく「111101」になります。これは、テストするすべての入力で発生します。欠落している最後の数字を除いて、すべての数字が正しい場所にあります。
どんな助けでもいただければ幸いです。
#include <stdio.h>
int main ()
{
int quotient = 123;
int i = 0;
int d1 = quotient % 2;
quotient = quotient / 2;
int c = 0;
int a = 0;
int number[32] = {};
while (quotient != 0)
{
i = i+1;
d1 = quotient % 2;
quotient = quotient / 2;
c++;
number[c]=d1;
}
for(a = 0; a < c; a = a + 1 )
{
printf("%d", number[c-a]);
}
return 0;
}
問題は、while
ループの前に1回分割していることです。
int d1 = quotient % 2;
quotient = quotient / 2;
それをただ:に置き換えてください
int d1 = 0;
そして物事はうまくいくはずです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加