Convert Decimal to Binary in C++

Here you will get the program to change over a number from decimal to double in C++.

How to change over the decimal number to pair?

Gap the number by 2 and spare the rest of.

Partition the remainder with 2 again and spare the rest of.

Rehash the procedure until 1 is left as remainder.

Presently compose the leftovers in switch request.

This program can just change over non-decimal numbers

Program

#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
The binary conversion of 5 is 101

Leave a Comment