#include <iostream>
#include <cmath>
using namespace std;
int binaryToDecimal(long long binary) {
int decimal = 0, i = 0, remainder;
while (binary != 0) {
remainder = binary % 10;
binary /= 10;
decimal += remainder * pow(2, i);
++i;
}
return decimal;
}
int decimalToOctal(int decimal) {
long long octal = 0;
int remainder, i = 1;
while (decimal != 0) {
remainder = decimal % 8;
decimal /= 8;
octal += remainder * i;
i *= 10;
}
return octal;
}
int main() {
long long binary;
cout << "Enter a binary number: ";
cin >> binary;
int decimal = binaryToDecimal(binary);
long long octal = decimalToOctal(decimal);
cout << "Binary: " << binary << " = Octal: " << octal << endl;
return 0;
}Output
Enter a binary number: 1010 Binary: 1010 = Octal: 12
Convert Binary to Octal in C++
This program teaches you how to convert a binary number directly to its octal equivalent in C++. This conversion can be done in two ways: either by converting binary→decimal→octal (two-step), or by grouping binary digits and converting directly. Understanding this conversion helps you work efficiently with different number systems and understand the relationships between binary, decimal, and octal.
What This Program Does
The program converts a binary number to an octal number. For example:
- Input binary: 1010
- Output octal: 12
The program uses a two-step approach: first converts binary to decimal, then decimal to octal. This is straightforward and reuses the conversion algorithms we've already learned.
Example:
- 1010 (binary) → 10 (decimal) → 12 (octal)
- 1111 (binary) → 15 (decimal) → 17 (octal)
Two-Step Conversion
Step 1: Binary to Decimal
- Converts the binary number to decimal first
- Uses the algorithm: multiply each digit by 2^position and sum
Step 2: Decimal to Octal
- Converts the decimal result to octal
- Uses the algorithm: repeatedly divide by 8 and collect remainders
Why Two Steps?:
- Simplest approach - reuses existing conversion algorithms
- Easy to understand and implement
- Can be optimized later with direct conversion
Direct Conversion Method (More Efficient)
How Direct Conversion Works:
- Group binary digits in sets of 3 (from right to left)
- Each group of 3 binary digits = 1 octal digit
- Convert each group directly to octal
Example (binary 1010 to octal):
- Group from right: 010 (pad with 0 if needed)
- 010 (binary) = 2 (octal)
- Next group: 1 (pad to 001)
- 001 (binary) = 1 (octal)
- Result: 12 (octal)
Why Groups of 3?:
- 2³ = 8, so 3 binary digits can represent 0-7 (all octal digits)
- This is the mathematical relationship between binary and octal
Summary
- Binary-to-octal conversion can be done in two steps: binary→decimal→octal.
- Two-step method is simple and reuses existing conversion algorithms.
- Direct conversion groups binary digits in sets of 3 for efficiency.
- 3 binary digits = 1 octal digit (because 2³ = 8).
- Understanding this conversion helps work with different number systems.
This program is essential for understanding number systems, efficient conversions, and the relationships between binary, decimal, and octal.