0

要求用户输入他想要的位数,并且还必须输入从最小(2 ^ 0)开始的每个位的值,程序应打印出用户以二进制形式给出的数字的十进制值。代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int bit,num,dec,i;
printf("insert number of bit:");//bit 2^0 is the first one on the left
scanf("%d",&bit);
num=0;
dec=0;
for( i=0;i<bit;i++){
    printf("insert %d bit:",i+1);
    scanf("%d",num);
    dec=dec+(pow(2,i)*num);
}
printf("your number in decimal would be:%d",dec);

return 0;
}
`
4

0 回答 0