当前位置:网站首页>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.
边栏推荐
- The Go language learning notes - dealing with timeout - use the language from scratch from Context
- 165.比较版本号
- 配合蓝牙打印的encoding-indexes.js文件内容:
- 卸载redis
- 100 latest software testing interview questions in 2022, summary of common interview questions and answers
- golang环境详细安装、配置
- 51单片机外设篇:DS18B20
- 51 MCU Peripherals: Infrared Communication
- el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
- 21天学习挑战赛安排
猜你喜欢
Browser onload event
ELK日志分析系统
MySQL 5.7 detailed download, installation and configuration tutorial
C语言小游戏——扫雷小游戏
Detailed explanation of interface in Go language
Packaging and deployment of go projects
51单片机外设篇:ADC
Cyber Security Learning - Intranet Penetration 4
Three methods of importing sql files in MySQL
Matlab论文插图绘制模板第41期—气泡图(bubblechart)
随机推荐
Detailed explanation of interface in Go language
51 MCU Peripherals: Infrared Communication
navicat无法连接mysql超详细处理方法
构造方法、成员变量、局部变量
Use the browser's local storage to realize the function of remembering the user name
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
面试官:设计“抖音”直播功能测试用例吧
Android studio connects to MySQL and completes simple login and registration functions
JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
非关系型数据库MongoDB的特点及安装
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
Detailed explanation of mysql stored procedure
卸载redis
Packaging and deployment of go projects
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
APP Bluetooth connection test of test technology
ATM系统
服务器的单机防御与集群防御
TikTok平台的两种账户有什么区别?
Detailed installation and configuration of golang environment