#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a[100];
int i=0,j=0,y=0,x;
cout<<"请输入任意正整数"<<endl;
cin>>x;
if(x>=2)
{
while((x/2)!=0)
{
a=(x%2);
i++;
x=(x/2);
}
for(j=0;j<=i;j++)
{
y=a[j]*pow(10,j)+y;
}
cout<<"转化为二进制的结果为:"<<y<<endl;
}
else
cout<<x<<endl;
return 0;
}
还有,第一个while循环,x/2是整数,截断取整,换成x/2.