Convert Binary to Decimal
C++ Program to Convert Binary to Decimal (5 Ways With Output)
IntermediateTopic: Advanced Number Programs
C++ Convert Binary to Decimal Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long long binary;
int decimal = 0, i = 0, remainder;
cout << "Enter a binary number: ";
cin >> binary;
long long temp = binary;
while (temp != 0) {
remainder = temp % 10;
temp /= 10;
decimal += remainder * pow(2, i);
++i;
}
cout << "Binary: " << binary << " = Decimal: " << decimal << endl;
return 0;
}Output
Enter a binary number: 1010 Binary: 1010 = Decimal: 10
Understanding Convert Binary to Decimal
This program converts binary to decimal by multiplying each digit by 2 raised to its position. It demonstrates 5 different methods: using pow(), using bit manipulation, using string, using recursion, and using stoi() with base 2.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.