# 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:

**Related Posts:**

**Program to convert binary number to octal number.****Program to convert binary number to hexadecimal number.****Program to convert octal number to binary number.****Program to convert octal number to decimal number.****Program to convert octal number to hexadecimal number.****Program to convert decimal number to binary number.****Program to convert decimal number to octal number.****Program to convert decimal number to hexadecimal number.****Program to convert hexadecimal number to binary number.****Program to convert hexadecimal number to octal number.****Program to convert hexadecimal number to decimal number.****Program to check Leap Year.****Program to find largest amongst three numbers.****Program to find factorial of a number.****Program to find GCD of two numbers.****Program to find LCM of two numbers.****Program to check whether entered number is odd or even.****Program to check whether entered number is prime number or not.****Program to check whether entered number is palindrome or not.****Program to check whether entered number is Armstrong Number or Not.**