当前位置:网站首页>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.
边栏推荐
猜你喜欢

Android Studio 实现登录注册-源代码 (连接MySql数据库)

MySQL 8.0.29 set and modify the default password

MySQL 5.7 detailed download, installation and configuration tutorial

18 years of programmer career, read more than 200 programming books, pick out some essence to share with you

ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了

整合ssm(一)

MYSQL unique constraint

自动化运维工具——ansible、概述、安装、模块介绍

Use the browser's local storage to realize the function of remembering the user name

Packaging and deployment of go projects
随机推荐
apifox介绍及使用(1)。
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
非关系型数据库MongoDB的特点及安装
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
MySQL导入sql文件的三种方法
Mysql implements optimistic locking
Use the browser's local storage to realize the function of remembering the user name
对node工程进行压力测试与性能分析
LeetCode brush topic series - 787 K station transfer within the cheapest flight
12 reasons for MySQL slow query
Brush LeetCode topic series - 10. Regular expression match
MySQL 多表关联一对多查询实现取最新一条数据
The Go language learning notes - dealing with timeout - use the language from scratch from Context
"Digital reconstruction of the system, getting the CEO is the first step"
MySql将一张表的数据copy到另一张表中
classSR论文阅读笔记