当前位置:网站首页>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;
}
代码复盘
通过本题熟悉十进制和二进制的转换方法。
边栏推荐
- golang泛型
- 21天学习挑战赛安排
- Navicat报错:1045-Access denied for user [email protected](using passwordYES)
- golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
- interrupt()、interrupted()和isInterrupted()你真的懂了吗
- Mysql存储json格式数据
- prisma使用mongodb副本集群报错引发的一些列问题
- "Digital reconstruction of the system, getting the CEO is the first step"
- ELK log analysis system
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
猜你喜欢
随机推荐
prisma使用mongodb副本集群报错引发的一些列问题
MySQL 8.0.28 version installation and configuration method graphic tutorial
高防服务器防御的原理是什么
c语言:查漏补缺(三)
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
21天学习挑战赛安排
CAN光端机解决泰和安TX3016C消防主机长距离联网问题 实现CAN与光纤之间的双向数据智能转换
MySQL如何创建用户
The Go language learning notes - dealing with timeout - use the language from scratch from Context
MySQL 8.0.29 解压版安装教程(亲测有效)
ORA-04044:此处不允许过程、函数、程序包或类型,系统分析与解决
本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
MySQL 5.7详细下载安装配置教程
golang generics
MySQL 5.7 detailed download, installation and configuration tutorial
[PSQL] window function, GROUPING operator
2022年100道最新软件测试面试题,常见面试题及答案汇总
自动化运维工具——ansible、概述、安装、模块介绍
mysql 查询表 所有字段
12个MySQL慢查询的原因分析









