编程语言
首页 > 编程语言> > C++用短除法把十进制转换为二进制输出

C++用短除法把十进制转换为二进制输出

作者:互联网

#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main() {
    int n;
    int ret[32];
    int i = 0;

    cout << "请输入一个正整数:";
    cin >> n;

    if (n < 0) {
        cout << "需要输入一个正整数!" << endl;
        system("pause");
        return 1;
    }

    while (n != 0) {
        ret[i] = n % 2;
        n = n / 2;
        i++;
    }

    for (i--; i >= 0; i--) {
        cout << ret[i] ;
    }
    cout << endl;
    system("pause");
    return 0;
}

 

标签:std,除法,cout,int,32,C++,--,include,十进制
来源: https://www.cnblogs.com/smartlearn/p/16563375.html