由键盘输入一个小于256的十进制正整数,然后将该数转换成八位的二进制数形式输出
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
int b0,b1,b2,b3,b4,b5,b6,b7;
printf("Please input a integer number:");
scanf("%d",&num);
b0=num%2;
b1=num/2%2;
b2=num/4%2;
b3=num/8%2;
b4=num/16%2;
b5=num/32%2;
b6=num/64%2;
b7=num/128%2;
printf("%d's Binary is:%1d%1d%1d%1d%1d%1d%1d%1d\n",num,b7,b6,b5,b4,b3,b2,b1,b0);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,n,i,b[8];
printf("Please input a integer number:");
scanf("%d",&num);
for(i=1;i<8;i++)
{
n=pow(2,i); /*pow在BC/TC下调试通过,系统自动强制转换,但VC下会出错,形参应为double或float*/
b[i]=(num/n)%2;
}
for(i=7;i>=0;i--)
printf("%d",b[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n=2,i=0,j,a[8];
printf("Please input a integer number(m>=0 and m<=255):");
scanf("%d",&m);
while(m<0&&m>255)
{
printf("Please input a number(m>=0 and m<=255):");
scanf("%d",&m);
}
while(m!=0)
{
a[i]=m%2;
i++;
m=m/2;
}
for(j=0;j<8-i;j++)
printf("0");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch();
}
#include<conio.h>
void main()
{
int num;
int b0,b1,b2,b3,b4,b5,b6,b7;
printf("Please input a integer number:");
scanf("%d",&num);
b0=num%2;
b1=num/2%2;
b2=num/4%2;
b3=num/8%2;
b4=num/16%2;
b5=num/32%2;
b6=num/64%2;
b7=num/128%2;
printf("%d's Binary is:%1d%1d%1d%1d%1d%1d%1d%1d\n",num,b7,b6,b5,b4,b3,b2,b1,b0);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,n,i,b[8];
printf("Please input a integer number:");
scanf("%d",&num);
for(i=1;i<8;i++)
{
n=pow(2,i); /*pow在BC/TC下调试通过,系统自动强制转换,但VC下会出错,形参应为double或float*/
b[i]=(num/n)%2;
}
for(i=7;i>=0;i--)
printf("%d",b[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n=2,i=0,j,a[8];
printf("Please input a integer number(m>=0 and m<=255):");
scanf("%d",&m);
while(m<0&&m>255)
{
printf("Please input a number(m>=0 and m<=255):");
scanf("%d",&m);
}
while(m!=0)
{
a[i]=m%2;
i++;
m=m/2;
}
for(j=0;j<8-i;j++)
printf("0");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch();
}