当前位置:网站首页>C language entry combat (13): decimal number to binary
C language entry combat (13): decimal number to binary
2022-08-02 06:19:00 【liber】
这是《C语言入门实战》系列的第13篇.
上一篇:C语言入门实战(12):求自然常数e的值
题目
从键盘输入任意一个正整数(≤255),Convert it to the corresponding8位二进制数,请编程实现.
要求
要求
输入输出格式示例1:
输入:160<回车>
输出:10100000
输入输出格式示例2:
输入:255<回车>
输出:11111111
输入输出格式示例3:
输入:13<回车>
输出:00001101
参考代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//为整型值nAllocate stack address
int n;
scanf("%d", &n);
int a[8] = {
0,0,0,0,0,0,0,0};
int idx = 7;
while(n>0 && idx>=0) {
a[idx] = n%2;
n /= 2;
idx--;
}
int i;
for(i=0;i<8;i++) {
printf("%d", a[i]);
}
return 0;
}
代码复盘
Familiarize yourself with the conversion method between decimal and binary through this question.
边栏推荐
猜你喜欢
随机推荐
MySQL 5.7 upgrade to 8.0 detailed process
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
Google 安装印象笔记剪藏插件
Google Chrome(谷歌浏览器)安装使用
浏览器的onload事件
Grid布局介绍
Stress testing and performance analysis of node projects
Go语言中定时任务库Cron使用详解
Go语言之interface详解
MySQL 5.7 detailed download, installation and configuration tutorial
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
12个MySQL慢查询的原因分析
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
C language: Check for omissions and fill in vacancies (3)
Packaging and deployment of go projects
Mysql return table









