当前位置:网站首页>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.
边栏推荐
猜你喜欢
Introduction to Grid Layout
[PSQL] window function, GROUPING operator
Stress testing and performance analysis of node projects
Detailed explanation of AMQP protocol
C language: Check for omissions and fill in vacancies (3)
本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
mysql 8.0.28版本安装配置方法图文教程
MySql copies data from one table to another table
51 MCU Peripherals: Infrared Communication
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
随机推荐
Navicat如何连接MySQL
mysql 8.0.28版本安装配置方法图文教程
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
[PSQL] 函数、谓词、CASE表达式、集合运算
TikTok平台的两种账户有什么区别?
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
CPU使用率和负载区别及分析
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
C语言中i++和++i在循环中的差异性
H5接入支付流程-微信支付&支付宝支付
Redis database
APP Bluetooth connection test of test technology
Navicat cannot connect to mysql super detailed processing method
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
Cyber Security Learning - Intranet Penetration 4
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
对node工程进行压力测试与性能分析
Google Chrome(谷歌浏览器)安装使用
21 Day Learning Challenge Schedule
Introduction and use of apifox (1).