Pages

Wednesday, November 18, 2015

C++ Program For Binary Equivalent Of A Decimal Number

The c++ program below finds the binary equivalent of an integer entered by the user in decimal form. The binary system often referred to as base two is made of only two digits which are o and 1. The decimal system as the most commonly used number system, has values ranging from 0 to 9. Normally, to convert a binary number to a decimal number, a division is made where the number is divided by 2 and the remainders either o or 1 are read from bottom to top.

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