```
#include <iostream>
#include <string>
using namespace std;
unsigned int pow(unsigned int a, unsigned int n);
int main()
{
string binary;
unsigned int decimal = 0;
cout << "Enter a binary number: ";
cin >> binary;
for(int i = binary.length()-1; i >= 0; i--) {
if(binary[i] > '1' || binary[i] < '0') {
cout << "Invalid binary number!" << endl;
return 1;
} else {
decimal += (binary[i] - '0') * pow(2, (binary.length()-i-1));
}
}
cout << "Decimal: " << decimal << endl;
return 0;
}
unsigned int pow(unsigned int a, unsigned int n)
{
unsigned int r = 1;
while( n-- > 0 ) r *= a;
return r;
}
```

**Are you able to help answer this sponsored question?**

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Recommended Articles