c++ 10进制转2进制

复制代码
#include <iostream>
using namespace std;

void fun(int n) {
    if (n == 0) {
        cout << 0;
        return;
    }

    int binary[32]; // 存储二进制数的数组
    int i = 0;

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

    // 逆序输出二进制数
    for (int j = i - 1; j >= 0; j--) {
        cout << binary[j];
    }
}

int main() {
    int decimal;
    cout << "10:";
    cin >> decimal;

    cout << "2:";
    fun(decimal);
    system("PAUSE");
    return 0;
}
复制代码

 

posted on   lydstory  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-06-29 鼎普会议文档系统
2023-06-29 金城保密
2023-06-29 深圳市同强信息技术有限责任公司
2022-06-29 802.11r包抓取
2022-06-29 goform
2022-06-29 XMLHttpRequest
2022-06-29 程序员的洁癖

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示