Wednesday, November 18, 2015
C++ Program For Binary Equivalent Of A Decimal Number
Also Read: C++ Program to Compute The Least Common Multiple of Integers
Also Read: C++ Program to Compute The Least Common Multiple of Integers
C++ Program For Binary Equivalent Of A Decimal Number
#include<iostream.h>
#include <stdio.h>
int main()
{
int n,x,i, j, k;
cout<<"Enter an integer in the decimal number system: ";
cin>>n;
x=n; //assign the value entered for x to n
cout<<"Binary equivalent of the "<<x<<" "<<"is: ";
for( i=1;x!=0;i++)
{
x=x/2;
}
i=i-2;
for (j= i; j>= 0; j--)
{
k = n >> j;
if (k & 1)
cout<<"1";
else
cout<<"0";
}
return 0;
}
Also Read: C++ Program to Check If a Number Is A Perfect Number or Not