我是编程的初学者,我试图理解将十进制转换为二进制的代码。代码如下:
#include <stdio.h>
int main()
{
long int DN,quo;
int rem[106],i=1,j;
printf("give me a decimal number: ");
scanf("%ld", &DN);
quo = DN;
while(quo!=0)
{
rem[i]= quo % 2;
quo = quo / 2;
i++;
}
printf("Equivalent binary value of your decimal number %ld\n: ",DN);
for(j = i-1 ;j> 0;j--)
printf("%d",rem[j]);
}
谁能帮助我理解以while循环开头的部分?我很抱歉发布这样一个简单的问题。
最佳答案
假设输入的十进制数字为13
Step 1. 13/2 , Remainder = 1, Quotient = 6
Step 2. 6/2 , Remainder = 0, Quotient = 3
Step 3. 3/2 , Remainder = 1, Quotient = 1
Step 4. 1/2 , Remainder = 1, Quotient = 0
现在,13的二进制等效项是反序的余数:1101