Here you will get program to convert a number from decimal to binary in C++.
在这里,您将获得在C ++中将数字从十进制转换为二进制的程序。
How to convert decimal number to binary?
如何将十进制数转换为二进制?
Divide the number by 2 and save the remainder somewhere.
将数字除以2,然后将其余部分保存在某处。
Divide the quotient with 2 again and save the remainder somewhere.
再次将商除以2,并将余数保存到某处。
Repeat the process until 1 is left as quotient.
重复该过程,直到将1保留为商。
Now write the remainders in reverse order.
现在,以相反的顺序写余数。
This program can only convert non decimal numbers.
该程序只能转换非十进制数字。
C ++中的十进制到二进制程序 (Program for Decimal to Binary in C++)
#include<iostream>
using namespace std;
int main()
{
int d,n,i,j,a[50];
cout<<"Enter a number:";
cin>>n;
cout<<"\nThe binary conversion of "<<n<<" is 1";
for(i=1;n!=1;++i)
{
d=n%2;
a[i]=d;
n=n/2;
}
for(j=i-1;j>0;--j)
cout<<a[j];
return 0;
}
Output
输出量
Enter a number:5
输入数字:5
The binary conversion of 5 is 101
5的二进制转换为101
翻译自: https://www.thecrazyprogrammer.com/2011/03/c-program-to-convert-decimal-number-to-2.html