当前位置:网站首页>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;
}
代码复盘
通过本题熟悉十进制和二进制的转换方法。
边栏推荐
- Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
- Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
- pg数据库报错问题,有懂的吗
- SQL数据表增加列
- What do interview test engineers usually ask?The test supervisor tells you
- MYSQL 唯一约束
- Navicat cannot connect to mysql super detailed processing method
- Navicat报错:1045-Access denied for user [email protected](using passwordYES)
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
- 浏览器的onload事件
猜你喜欢

"Digital reconstruction of the system, getting the CEO is the first step"

【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组

Detailed explanation of the software testing process (mind map) of the first-tier manufacturers

Three methods of importing sql files in MySQL

18年程序员生涯,读了200多本编程书,挑出一些精华分享给大家

Mycat2.0搭建教程

navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)

ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution

说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~

pg数据库报错问题,有懂的吗
随机推荐
ELK日志分析系统
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
navicat无法连接mysql超详细处理方法
Detailed explanation of AMQP protocol
MySQL 8.0.28 version installation and configuration method graphic tutorial
数学建模学习笔记:层次分析法(AHP)
go语言中的goroutine(协程)
Mysql implements optimistic locking
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
Mysql实现乐观锁
JUC(一)- JUC学习概览 - 对JUC有一个整体的认识
Grid布局介绍
The Go language learning notes - dealing with timeout - use the language from scratch from Context
leetcode 665. Non-decreasing Array 非递减数列(中等)
LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
golang泛型
Go语言之interface详解
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
Mycat2.0搭建教程