C++ Program to Convert Binary Number to Decimal

In this program, you'll learn to convert Binary number to Decimal number system.

Decimal to binary in C: We can convert any decimal number (base-10 (0 to 9)) into binary number(base-2 (0 or 1)) by c program.

Decimal number is a base 10 number, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.

Binary number is a base 2 number contains only 2 digits either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.



Source Code
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int dec = 0, i, rem, c = 0;
    long bin;
    cout<<"Enter the Binary number: ";
    cin>>bin;

    while(bin > 0)
    {
        rem = bin % 10;
        dec = dec + rem * pow(2, c);
        bin = bin / 10;
        c++;
    }
    cout<<"Decimal Equivalent is: "<<dec;
    return 0;
}
Output
Enter the Binary number: 101011
Decimal Equivalent is: 43

Enter the Binary number: 101
Decimal Equivalent is: 5