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

Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)

Navicat cannot connect to mysql super detailed processing method

浏览器的onload事件

AMQP协议详解

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

MYSQL 唯一约束

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

非关系型数据库MongoDB的特点及安装

Android studio connects to MySQL and completes simple login and registration functions

How much does a test environment cost? Start with cost and efficiency
随机推荐
为什么4个字节的float要比8个字节的long大呢?
TikTok平台的两种账户有什么区别?
Timing task library in the language use Cron, rounding
C language: Check for omissions and fill in vacancies (3)
21 Day Learning Challenge Schedule
MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
C语言入门实战(13):十进制数转二进制
How much does a test environment cost? Start with cost and efficiency
MySQL 8.0.28 version installation and configuration method graphic tutorial
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
Go语言中定时任务库Cron使用详解
51单片机外设篇:点阵式LCD
Mysql common commands
C语言中i++和++i在循环中的差异性
MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)
Stress testing and performance analysis of node projects
【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
ELK log analysis system