Convert Binary to Decimal
C++ Program to Convert Binary to Decimal (5 Ways With Output)
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;
}Enter a binary number: 1010 Binary: 1010 = Decimal: 10
Understanding Convert Binary to Decimal
This program teaches you how to convert a binary number to its decimal (base-10) equivalent in C++. Binary is the base-2 number system used by computers, where each digit can only be 0 or 1. Understanding binary-to-decimal conversion is fundamental for computer science, digital systems, and low-level programming.
---
1. What This Program Does
The program converts a binary number (base-2) to a decimal number (base-10). For example:
Binary numbers use only digits 0 and 1, while decimal numbers use digits 0-9. The conversion involves multiplying each binary digit by 2 raised to its position power and summing the results.
Example:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
long long binary;
int decimal = 0, i = 0, remainder;
---
4. Taking Input From the User
The program asks:
cin >> binary;
We also create a temporary copy:
long long temp = binary;
This is important because we'll modify the number during conversion, and we need the original for display.
---
cout << "Enter a binary number: ";5. The Conversion Algorithm
The core conversion uses a while loop:
while (temp != 0) {
remainder = temp % 10;
temp /= 10;
decimal += remainder * pow(2, i);
++i;
}
Let's break down how this works step-by-step for binary = 1010:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
---
6. Understanding the Binary System
Positional Value System
:
In binary, each position represents a power of 2:
Reading Binary from Right to Left
:
Example Calculation
:
Binary 1010:
---
7. Key Operations Explained
Modulo Operator (%)
:
Integer Division (/)
:
Power Function (pow())
:
Accumulation
:
---
8. Other Methods (Mentioned but not shown in code)
Method 2: Using Bit Manipulation
int decimal = 0;
int base = 1;
while (binary > 0) {
int lastDigit = binary % 10;
decimal += lastDigit * base;
base *= 2;
binary /= 10;
}
Method 3: Using String
string binaryStr = to_string(binary);
int decimal = 0;
for (int i = 0; i < binaryStr.length(); i++) {
int digit = binaryStr[i] - '0';
decimal += digit * pow(2, binaryStr.length() - 1 - i);
}
Method 4: Using Recursion
int binaryToDecimal(long long binary, int i = 0) {
if (binary == 0) return 0;
}
return (binary % 10) * pow(2, i) + binaryToDecimal(binary / 10, i + 1);Method 5: Using stoi() with Base 2
string binaryStr = to_string(binary);
int decimal = stoi(binaryStr, nullptr, 2);
---
9. Displaying the Result
The program prints:
Output:
This clearly shows both the original binary number and its decimal equivalent.
---
cout << "Binary: " << binary << " = Decimal: " << decimal << endl;10. Understanding Number Bases
Decimal (Base-10)
:
Binary (Base-2)
:
Why Binary?
:
---
11. Common Binary Values
Powers of 2
:
Common Binary to Decimal
:
---
12. When to Use Each Method
-
pow() Method
: Good for learning - clear and straightforward.
-
Bit Manipulation
: Best for efficiency - avoids pow() overhead, recommended.
-
String Method
: Good for learning string operations - processes left to right.
-
Recursion
: Educational - helps understand recursive thinking.
-
stoi() Method
: Simplest - one line, but requires string conversion.
Best Practice
: Use bit manipulation method for efficiency, or stoi() for simplicity.
---
13. Important Considerations
Valid Binary Digits
:
Large Binary Numbers
:
Reading Direction
:
---
14. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning number systems, understanding how computers represent numbers, and preparing for more advanced topics in computer science and digital systems in C++ programs.
Let us now understand every line and the components of the above program.
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.
Practical Learning Notes for Convert Binary to Decimal
This C++ program is part of the "Advanced Number Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.