欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
}