C语言,进制转换之十进制转二进制,完整代码
程序员文章站
2022-07-15 09:36:10
...
#include "iostream"
using namespace std;
#define MAX 50
int main()//十转二
{
cout << "请输入一个整型十进制数:"<<endl;
int number;
cin >> number;
int elem2[MAX];
int i = 0;
int length=0;
do
{
elem2[i] = number % 2;
number = number / 2;
i++;
length++;
} while (number!= 1);
elem2[length] = number;
length++;
for (i = length-1; i>=0;i--)
cout << elem2[i] << " ";
cout << endl;
return 0;
}
上一篇: 十进制整数、小数转二进制