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

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法

Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)

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

mysql实现按照自定义(指定顺序)排序

公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
![[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays](/img/eb/9b05508e88b7f17d80de2afa8c08ce.png)
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays

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

在腾讯做外包测试的那些日子.....

Grid布局介绍

H5接入支付流程-微信支付&支付宝支付
随机推荐
软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
go项目的打包部署
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
Grid布局介绍
去字节跳动自动化测试二面原题(根据录音整理)真实有效 26
PSQL function, predicate, CASE expression and set operations
MySQL大批量造数据
MySQL multi-table association one-to-many query to get the latest data
卸载redis
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
Brush LeetCode topic series - 10. Regular expression match
Mycat2.0搭建教程
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
Mysql 回表
面试官:设计“抖音”直播功能测试用例吧
Redis集群模式
nacos注册中心
51 MCU Peripherals: Infrared Communication
Redis数据库
apisix-入门使用篇