Convert Binary to Decimal in C++

Here you will learn how to convert binary to decimal in C++.

 

We can convert a binary number into decimal in following way.
  • Multiply each digit from right to left by power of 2. Here the power of 2 will be the position of the digit starting from 0.
  • Now add all the values to obtain decimal number.
 

Convert Binary to Decimal in C++

 

Output
Enter any Binary number:111
The Decimal conversion of 111 is 7

2 thoughts on “Convert Binary to Decimal in C++

  1. Fatal1ty

    i have seen this approach on the net many times, can u give us something new….like another method to perform this particular conversion ..

    Reply
    1. Abhishek Roy

      /*This is another approach, but this has a flaw while used in C++. This code works perfectly in C language.
      This program uses the Algo with the least no of executable lines in C to perform the binary to decimal conversion, hence the quickest known to me. To know about the flaw or any doubt in the code U can ask me on this email id : abhishekroy.id@gmail.com*/

      #include

      void main()
      {
      long int bnum,dnum=0,base=1,rem;

      cout<<"Enter the binary number : ";
      cin>>bnum;

      while(bnum!=0)
      {
      rem = bnum%10;
      dnum = dnum+rem*base;
      base = base*2;
      bnum = bnum/10;
      }
      cout<<"nEquivalent decimal value : "<<dnum;
      }

      //cheers!!!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *