A Program to Convert Binary to Decimal
In this example, we are giving a program to convert a number from binary to decimal.
Program to convert binary to decimal:
#include <iostream>
#include <cmath>
using namespace std;
int convert(long long n);
int main() {
long long n;
cout << "Enter a binary number: ";
cin >> n;
cout << n << " in binary = " << convert(n) << " in decimal " << endl;
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
Output:
Enter a binary number: 11011 11011 in binary = 27 in decimal