当前位置:网站首页>C language: mathematical method of converting decimal system into binary system
C language: mathematical method of converting decimal system into binary system
2022-07-28 14:47:00 【Youth is short!】
First of all, we need to clarify the logic : How to convert decimal system to binary system
Our approach —— Short division
In decimal digits 10 For example
Our goal is to convert the decimal number into binary number
The pure mathematical method is as follows :
10÷2=5 more than 0
5÷2=2 more than 1
2÷2=1 more than 0
1÷2=0 more than 1
Then write the remainder from bottom to top , namely 1010 You can get 10 Corresponding binary data
The specific code implementation is as follows :
#include<stdio.h>
int main()
{
int a,b,k,i;
int remainder[30];
while(1)
{
// Defined a remainder Array , Used to collect the remainder of short division , Stack output in reverse order .
printf(" Please enter a decimal number : ");
scanf("%d",&a);
k=0;
while(a!=0)
{
b=a/2;
k++;
remainder[k]=a-b*2;
a=a/2;
};
printf(" Converting to binary numbers is : ");
for (i=k; i>=1; i--)
{
printf("%d",remainder[i]);
}
printf("\n");
}
return 0;Of course, there are simpler itoa Function to implement , But that's the application method , This article only introduces the basic mathematical method implementation
边栏推荐
- Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
- Another way of understanding the essence of Hamming code
- Copy excel row to specified row
- Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
- Bulk Rename Utility
- C# 获取当前路径7种方法
- 35道MySQL面试必问题图解,这样也太好理解了吧
- 面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
- Iterator iterator interface
- Afnetworking crash course
猜你喜欢
随机推荐
Redis configuration file explanation
Penguin side: why not recommend using select *?
ssh服务
OKR and grad
2022年熔化焊接与热切割考题及在线模拟考试
Core Data 是如何在 SQLite 中保存数据的
一些企业数据平台建设的思考
Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
多线程顺序运行有几种方法?
Qt中QTableView设置分页显示的三种方法[通俗易懂]
BGP experiment
Ability to add class @published for custom attribute wrapper types
数字化转型安全问题频发,山石网科助力数字政府建设
使用Weka与Excel进行简单的数据分析
如何在 Core Data 中进行批量操作
468 product planning and promotion plan (150 copies)
力扣解法汇总1331-数组序号转换
[ecmascript6] async and await
工厂模式和构造函数模式
[leetcode] 1331. Array sequence number conversion









