当前位置:网站首页>C语言入门实战(13):十进制数转二进制
C语言入门实战(13):十进制数转二进制
2022-08-02 05:06:00 【liberg】
这是《C语言入门实战》系列的第13篇。
上一篇:C语言入门实战(12):求自然常数e的值
题目
从键盘输入任意一个正整数(≤255),将其转换为相应的8位二进制数,请编程实现。
要求
要求
输入输出格式示例1:
输入:160<回车>
输出:10100000
输入输出格式示例2:
输入:255<回车>
输出:11111111
输入输出格式示例3:
输入:13<回车>
输出:00001101
参考代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//为整型值n分配栈地址
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;
}
代码复盘
通过本题熟悉十进制和二进制的转换方法。
边栏推荐
- leetcode 204. Count Primes 计数质数 (Easy)
- ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
- 非关系型数据库MongoDB的特点及安装
- interrupt()、interrupted()和isInterrupted()你真的懂了吗
- matlab simulink 模糊pid结合smith控制温度
- Detailed explanation of AMQP protocol
- MySQL 游标
- Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
- Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
- JDBC revisited
猜你喜欢
matlab simulink 飞机飞行状态控制
大屏UI设计-看这一篇就够了
classSR论文阅读笔记
面试测试工程师一般会问什么?测试主管告诉你
整合ssm(一)
数学建模学习笔记:层次分析法(AHP)
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
18年程序员生涯,读了200多本编程书,挑出一些精华分享给大家
力扣 2127. 参加会议的最多员工数 拓扑剪枝与2360补充
随机推荐
认识CAN光纤转换器的光纤接口和配套光纤线缆
Detailed installation and configuration of golang environment
MySQL 8.0.29 解压版安装教程(亲测有效)
The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26
golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
PSQL function, predicate, CASE expression and set operations
MySQL String Concatenation - Various String Concatenation Practical Cases
apisix-Getting Started
简道云-灵活易用的应用搭建平台
CPU使用率和负载区别及分析
MySQL 8.0.29 设置和修改默认密码
21 Day Learning Challenge Schedule
高防服务器防御的原理是什么
Packaging and deployment of go projects
非关系型数据库MongoDB的特点及安装
JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
MySQL 5.7升级到8.0详细过程
JUC(一)- JUC学习概览 - 对JUC有一个整体的认识
LeetCode brush topic series - 787 K station transfer within the cheapest flight