用C++编写一个十进制转换成二进制的程序?

来源:百度知道 编辑:UC知道 时间:2024/05/26 15:20:57
只能用数组,FOR循环,SWITCH循环,DO-WHILE循环,INT,VOID命令!

#include<iostream.h>
void trans(long n,int base)
{int b,i=0, a[24];
while(n>0)
{b=n%base;
n=(n-b)/base;
a[i]=b;
i++;
}
for(b=i-1;b>=0;b--)
{
switch(a[b])
{case 0:
case 1:cout<<a[b];break;
default:break;
}}
cout<<endl;
}
void main()
{long x;
cout<<"input a integer :";
cin>>x;
trans(x,2);
}