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
                  C++ Program to Check If a Number Is A Perfect Number or Not
        



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;
}






Do you like this post? Please link back to this article by copying one of the codes below.

URL Of Post:


HTML Link Code:

BB (forum) link code:

Comments