Convert Binary to Octal

Convert Binary to Octal in C++ (5 Programs)

IntermediateTopic: Advanced Number Programs
Back

C++ Convert Binary to Octal Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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

Understanding Convert Binary to Octal

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.

---

1. 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)

---

2. Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides pow() function for calculating powers.
Used in the binary-to-decimal conversion.

---

3. Understanding the Two-Step Conversion

The program uses two helper functions:

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.

---

4. Function 1: binaryToDecimal()

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;

}

}

This function:

Takes a binary number as input.
Converts it to decimal using the standard algorithm.
Returns the decimal equivalent.
    return decimal;

How it works

(for binary = 1010):

Extracts digits from right to left.
Multiplies each digit by 2^position.
Sums all contributions: 0×1 + 1×2 + 0×4 + 1×8 = 10

---

5. Function 2: decimalToOctal()

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;

}

}

This function:

Takes a decimal number as input.
Converts it to octal using the standard algorithm.
Returns the octal equivalent.
    return octal;

How it works

(for decimal = 10):

Repeatedly divides by 8.
Collects remainders: 10÷8=1 rem 2, 1÷8=0 rem 1.
Builds octal: 2 + 1×10 = 12

---

6. Main Function - Combining Both Steps

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;

}

int main() {

Process Flow

:

1.User enters binary number (e.g., 1010).
2.Convert binary to decimal (1010 → 10).
3.Convert decimal to octal (10 → 12).
4.Display the result.

---

7. Direct Conversion Method (More Efficient)

Why Direct Conversion?

:

Two-step method works but is less efficient.
Direct conversion groups binary digits and converts directly.
More efficient: O(log n) vs O(log n) + O(log n).

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.

---

8. Other Methods (Mentioned but not shown in code)

Method 2: Direct Conversion

Groups binary digits in sets of 3.
Converts each group directly to octal digit.
More efficient than two-step method.

Method 3: Using bitset

bitset<32> binaryBits(binary);

string binaryStr = binaryBits.to_string();

// Group and convert

Uses bitset for binary representation.
Groups and converts to octal.
#include <bitset>

Method 4: Using String Manipulation

Converts binary to string.
Groups characters in sets of 3.
Converts each group to octal digit.

Method 5: Using Functions (Modular)

Encapsulates each conversion step in functions.
Makes code more organized and testable.
Easier to maintain and debug.

---

9. Displaying the Result

The program prints:

Output:

Binary: 1010 = Octal: 12

This clearly shows the conversion from binary to octal.

---

cout << "Binary: " << binary << " = Octal: " << octal << endl;

10. Understanding Binary-Octal Relationship

Why 3 Binary Digits = 1 Octal Digit?

:

Binary is base-2, octal is base-8.
2³ = 8, so 3 binary digits can represent 0-7 (all octal digits).
This makes direct conversion possible.

Conversion Table

(3-bit binary to octal):

000 (binary) = 0 (octal)
001 (binary) = 1 (octal)
010 (binary) = 2 (octal)
011 (binary) = 3 (octal)
100 (binary) = 4 (octal)
101 (binary) = 5 (octal)
110 (binary) = 6 (octal)
111 (binary) = 7 (octal)

Direct Conversion Example

:

Binary: 1 0 1 0
Group: 1 | 010

001 | 010 (pad left group with 0)

Octal: 1 | 2
Result: 12 (octal)

---

11. When to Use Each Method

-

Two-Step Conversion

: Best for learning - clear, reuses known algorithms.

-

Direct Conversion

: Best for efficiency - faster, groups digits directly.

-

bitset Method

: Good for working with binary data - uses C++ bitset class.

-

String Manipulation

: Flexible - good for very large numbers.

-

Functions

: Best for code organization - modular and maintainable.

Best Practice

: Use two-step for learning, direct conversion for efficiency.

---

12. Important Considerations

Padding with Zeros

:

When grouping binary digits, leftmost group may need padding.
Pad with leading zeros to make groups of 3.
Example: 1010 → group as 001 and 010.

Large Binary Numbers

:

Very large binary numbers may cause overflow in intermediate steps.
Consider using string-based methods for very large numbers.
Direct conversion is more efficient for large numbers.

Validation

:

Ensure binary input contains only 0s and 1s.
This program assumes valid binary input.

---

13. return 0;

This ends the program successfully.

---

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.
Multiple methods exist: two-step, direct, bitset, string, functions.
Choose method based on needs: learning vs. efficiency vs. code organization.

This program is fundamental for beginners learning number system conversions, understanding the relationships between binary, decimal, and octal, 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 Octal

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.

Table of Contents