Binary to Decimal in C++
We must create a software to convert a binary number into an equivalent decimal value given a binary number as input.
Example:
// C++ program to convert binary to decimal
#include < iostream >
#include < bits/sdtc++.h >
#include < stdlib >
using namespace std;
// Function to convert binary to decimal
int BinaryToDecimal ( int n )
{
int number = n ;
int decimal_value = 0 ;
// Initializing base value to 1, i.e 2^0
int base = 1 ;
int temp = number ;
while ( temp ) {
int last_digit = temp % 10 ;
temp = temp / 10 ;
decimal_value += last_digit * base ;
base = base * 2 ;
}
return decimal_value ;
}
// Driver program to test above function
int main ( )
{
int number = 111 ;
cout << BinaryToDecimal ( num ) << endl ;
}
OUTPUT:
7
Explanation:
The goal is to extract the digits of a binary number starting with the rightmost digit and store them in a variable called decimal value. Multiply the digit with the suitable base (Power of 2) and add it to the variable decimal value when extracting digits from the binary integer. Finally, the decimal number will be stored in the variable decimal value. If the binary number is 111, decimal value equals 1*(2^2) + 1*(2^1) + 1*(2^0) which is equal to 7.
NOTE: The software only accepts binary values in the integer range. If you need to work with lengthy binary integers, such as 20 bits or 30 bits, you can store them in a string variable.
Storing binary values in string variables rather than integers
For example:
// C++ program to convert binary to decimal
// when input is represented as binary string.
#include < iostream >
#include < string >
#include < bits/sdtc++.h >
#include < stdlib >
using namespace std ;
// Function to convert binary to decimal
int BinaryToDecimal ( string num )
{
string number = num ;
int decimal_value = 0 ;
// Initializing base value to 1, i.e 2 ^ 0
int base = 1 ;
int length = num.length ( ) ;
for ( int i = length - 1 ; i >= 0 ; i-- ) {
if ( number [ i ] == '1' )
decimal_value += base ;
base = base * 2 ;
}
return decimal_value ;
}
// Driver program to test above function
int main ( )
{
string number = "111" ;
cout << BinaryToDecimal ( number ) << endl ;
}
OUTPUT:
7
Explanation:
In the above program it is using string to store the binary number i.e. 111 in the string number, and performing the same function as we did in the program using integer to store the binary number.
Using Pre-Defined Function
//C++ program to convert binary to decimal
//using pre defined function
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
int main ( )
{
char BinaryNumber [ ] = "111" ;
cout << stoi ( BinaryNumber , 0 , 2 ) ;
return 0 ;
}
OUTPUT:
7
Explanation:
In the above program, stoi function is a predefined function in C++, which helps in converting binary to decimal number in C++;
Another Example:
// C++ program to convert binary to decimal
#include < iostream >
#include < bits/stdc++.h >
#include < cmath >
using namespace std ;
// function prototype
int Binarytodecimal ( long long ) ;
int main ( ) {
long long numver ;
cout << "enter a binary number: " ;
cin >> number ;
cout << number << " in binary = " << Binarytodecimal ( number ) << " in decimal" ;
return 0 ;
}
// function definition
int Binarytodecimal ( long long number ) {
int decimal = 0 , i = 0 , remainder ;
while ( number != 0 ) {
remainder = number % 10 ;
number /= 10 ;
decimal += rem * pow (2 , i ) ;
++i ;
}
return decimal ;
}
OUTPUT:
Enter a binary number: 111
111 in binary = 7 in decimal
……………………………………….
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
The header file cmath has been included in the program to conduct mathematical calculations. We ask the user to provide a binary number that will be converted to decimal using the Binarytodecimcal ( ) method.
Let’s look at another example to properly understand the concept:
#include <iostream>
#include <cmath>
using namespace std;
long long Binarytodecimal (int);
int main() {
int n, bin;
cout << "enter a decimal number: ";
cin >> n;
bin = Binarytodecimal(n);
cout << n << " in decimal = " << bin << " in binary" << endl ;
return 0;
}
long long Binarytodecimal(int n) {
long long bin = 0;
int rem, i = 1;
while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
OUTPUT:
Enter a decimal number: 10
10 in decimal = 1010 in binary
Explanation:
In this above program we are using a while loop for our iteration in the function called Binarytodecimal.