# Binary to Decimal Number Conversion

“** Binary to Decimal Number Conversion**” is one of the classic programming problem exercise. Here, we are given a binary number, entered by user and our task is to write a program to convert the given binary number to its equivalent decimal number.

**Example:**

Binary = 111INPUT:Decimal = 7OUTPUT:Binary = 100INPUT:Decimal = 4OUTPUT:

To convert binary number into decimal, we will scan the number from right to left and put it into formula.

FORMULA TO CONVERT BINARY TO DECIMAL:2^0 * (last digit) + 2^1 * (second last digit) + ….

**C++ Program to convert binary to decimal is as follows:**

#include<bits/stdc++.h> using namespace std; long int convert(long int); int main() { // Scan the binary number long int binary; cout<<"Enter the binary number : "; cin>>binary; //Call the convert function long int decimal = convert(binary); //Printing the decimal equivalent cout<<"The decimal equivalent of "<<binary<<" is "<<decimal; } long int convert(long int binary) { long int res = 0; int i = 0; while(binary > 0) { int digit = binary % 10; /* Extracting the last digit */ res = res + (digit * pow(2,i)); /* Putting it into formula */ binary = binary/10; i++; } return res; }

Enter the binary number : 111 The decimal equivalent of 111 is 7OUTPUT: