当前位置:网站首页>C语言入门实战(13):十进制数转二进制
C语言入门实战(13):十进制数转二进制
2022-08-02 05:06:00 【liberg】
这是《C语言入门实战》系列的第13篇。
上一篇:C语言入门实战(12):求自然常数e的值
题目
从键盘输入任意一个正整数(≤255),将其转换为相应的8位二进制数,请编程实现。
要求
要求
输入输出格式示例1:
输入:160<回车>
输出:10100000
输入输出格式示例2:
输入:255<回车>
输出:11111111
输入输出格式示例3:
输入:13<回车>
输出:00001101
参考代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//为整型值n分配栈地址
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;
}
代码复盘
通过本题熟悉十进制和二进制的转换方法。
边栏推荐
猜你喜欢

100 latest software testing interview questions in 2022, summary of common interview questions and answers

C language: Check for omissions and fill in vacancies (3)

MySQL大批量造数据

Detailed explanation of AMQP protocol

25K测试老鸟6年经验的面试心得,四种公司、四种问题…
![[PSQL] 窗口函数、GROUPING运算符](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] 窗口函数、GROUPING运算符

数学建模学习笔记:层次分析法(AHP)

MySQL安装教程

MySql copies data from one table to another table

goroutine (coroutine) in go language
随机推荐
[PSQL] 函数、谓词、CASE表达式、集合运算
CPU使用率和负载区别及分析
高防服务器防御的原理是什么
MySQL导入sql文件的三种方法
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
SQL数据表增加列
Go语言之interface详解
MySQL 8.0.29 解压版安装教程(亲测有效)
一线大厂软件测试流程(思维导图)详解
Android Studio 实现登录注册-源代码 (连接MySql数据库)
MySQL安装常见报错处理大全
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
Matlab学习第二天
MySQL 8.0.28 version installation and configuration method graphic tutorial
The Go language learning notes - dealing with timeout - use the language from scratch from Context
Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言
腾讯注册中心演进及性能优化实践
12 reasons for MySQL slow query
认识消防报警联网中CAN光纤转换器的光纤接口和配套光纤线缆
MySQL夺命10问,你能坚持到第几问?