在C ++中将十进制转换为二进制

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

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值