codescracker
c++

C++ Program to Convert Binary to Decimal



« Previous Program Next Program »

In this article, you will learn and get code on binary to decimal conversion with and without using function in C++.

But before going through the program, if you are not aware about steps, formula, or logic used behind the conversion of binary to decimal, then you can refer to binary to decimal conversion. There you will get everything about it in very short time.

Binary to Decimal without Function

To convert any number entered by user (in binary number system) to its equivalent decimal value in C++ programming, you have to ask from user to enter the binary number first. And then apply the logic and convert it into its equivalent decimal value as shown in the program given below.

Let's have a look at the program first, will explain it later on.

#include<iostream>
using namespace std;
int main()
{
    int binnum, decnum=0, i=1, rem;
    cout<<"Enter any Binary Number: ";
    cin>>binnum;
    while(binnum!=0)
    {
        rem = binnum%10;
        decnum = decnum + (rem*i);
        i = i*2;
        binnum = binnum/10;
    }
    cout<<"\nEquivalent Decimal Value = "<<decnum;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program convert number from binary to decimal

Now supply any input as binary number say 1111010 and press ENTER key to see its equivalent value in decimal as shown in the output given below:

binary to decimal c++

Program Explained with Dry Run

The dry run of above program with user input 1111010 goes in a way that:

  1. Initially decnum=0, i=1 and binnum=1111010 (entered by user)
  2. The condition binnum!=0 (inside the while loop) or 1111010!=0 evaluates to be true
  3. Therefore program flow goes inside the loop
  4. And binnum%10 or 1111010%10 or 0 gets initialized to rem. Now rem=0
  5. Similarly, decnum+(rem*i) or 0+(0*1) or 0 gets initialied to decnum. Now decnum=0
  6. Then i*2 or 1*2 or 2 gets initialized to i. Now i=2
  7. And at last, binnum/10 or 1111010/10 or 111101 gets initialized to binnum. Now binnum=111101
  8. After executing all the four statements of while loop (for first time), we have decnum=0, i=2 and binnum=111101
  9. Program flow goes back to the condition of while loop
  10. Again the condition binnum!=0 or 111101!=0 evalutes to be true, therefore program flow again goes inside the loop
  11. And binnum%10 or 111101%10 or 1 gets initialized to rem. Now rem=1
  12. Similarly, decnum+(rem*i) or 0+(1*2) or 2 gets initialized to decnum. Now decnum=2
  13. And i*2 or 2*2 or 4 gets initialized to i. Now i=4
  14. And at last, binnum/10 or 111101/10 or 11110 gets initialized to binnum. Now binnum=11110
  15. Now process from step no.9 to 14 with new value of decnum, i and binnum
  16. Now we have decnum=2, i=8 and binnum=1111
  17. Go to step no.15
  18. Now we have decnum=10, i=16, and binnum=111
  19. Go to step no.15
  20. Now we have decnum=26, i=32, and binnum=11
  21. Go to step no.15
  22. Now we have decnum=58, i=64, and binnum=1
  23. Go to step no.15
  24. Now we have decnum=122, i=128, binnum=0

Dry Run in Tubular Form

While Loop Evaluation rem decnum i binnum
1st Evaluation 0 0 2 111101
2nd Evaluation 1 2 4 11110
3rd Evaluation 0 2 8 1111
4th Evaluation 1 10 16 111
5th Evaluation 1 26 32 11
6th Evaluation 1 58 64 1
7th Evaluation 1 122 128 0

You can also print the value of all four variables say rem, decnum, i, and binnum during the execution of while loop by placing the following statement:

cout<<"\nrem\tdecnum\ti\tbinnum\n";

just before the while loop. And the following statement:

cout<<rem<<"\t"<<decnum<<"\t"<<i<<"\t"<<binnum<<endl;

just after the fourth statement of while loop's body. Now with user input 1111010, output looks like:

binary to decimal conversion c++

Binary to Decimal using Function

This program converts binary to decimal using a user-defined function, BinToDec(). It takes binary number as its argument, and returns its equivalent decimal value.

This program uses pow() function. It takes two argument, the first argument is known for base, whereas the second is known for exponent. Therefore, pow(2, 5) means 25, that equals, 32.

#include<iostream>
#include<math.h>
using namespace std;
int BinToDec(int bin);
int main()
{
    int binnum, decnum;
    cout<<"Enter any Binary Number: ";
    cin>>binnum;
    decnum = BinToDec(binnum);
    cout<<"\nEquivalent Decimal Value = "<<decnum;
    cout<<endl;
    return 0;
}
int BinToDec(int bin)
{
    int dec=0, i=0, rem;
    while(bin!=0)
    {
        rem = bin%10;
        dec = dec + rem*pow(2,i);
        i++;
        bin = bin/10;
    }
    return dec;
}

This program produces the same output as of previous program. You can check with dry run on your own. To learn more about function in C++, you can refer its separate tutorial.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker