从键盘输入一个二进制非负整数,屏幕上打印输出对应的十进制、八进制和十六进制数,要求输出的十六进制数中的英文字母为大写字母。 示例输入、输出如下: Input a binary number: 100110101101 The number is 2477 in decimal. The number is 4655 in octal. The number is 9AD in hexadecimal.
#include <stdio.h> #include <string.h> char s[128]; int main(){ printf("Input a binary number:"); gets(s); int len,i,n=0; len=strlen(s); for (i=0; i<len; i++) { if(s[i]=='1'){ n<<=1; n|=1;
} else{ n<<=1; } } printf("The number is %d in decimal.\n",n); printf("The number is %o in octal.\n",n); printf("The number is %X in octal.\n",n); }本回答被提问者和网友采纳