当前位置:网站首页>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.
边栏推荐
- Google Chrome(谷歌浏览器)安装使用
- Navicat如何连接MySQL
- classSR论文阅读笔记
- Navicat报错:1045-Access denied for user [email protected](using passwordYES)
- Detailed explanation of mysql stored procedure
- Go语学习笔记 - grpc serverclient protobuf 从零开始Go语言
- 软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
- CPU使用率和负载区别及分析
- navicat新建数据库
- The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
猜你喜欢
随机推荐
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
Detailed installation and configuration of golang environment
Google 安装印象笔记剪藏插件
Mysql common commands
Install and use Google Chrome
MySQL 8.0.29 设置和修改默认密码
软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
100 latest software testing interview questions in 2022, summary of common interview questions and answers
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
Introduction to Grid Layout
MYSQL unique constraint
JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
Go language study notes - grpc serverclient protobuf Go language from scratch
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
配合蓝牙打印的encoding-indexes.js文件内容:
MySQL 8.0.28 version installation and configuration method graphic tutorial
[PSQL] 函数、谓词、CASE表达式、集合运算
golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法









