Convert Octal to Binary

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

IntermediateTopic: Advanced Number Programs
Back

C++ Convert Octal to Binary 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 octalToDecimal(long long octal) {
    int decimal = 0, i = 0, remainder;
    while (octal != 0) {
        remainder = octal % 10;
        octal /= 10;
        decimal += remainder * pow(8, i);
        ++i;
    }
    return decimal;
}

long long decimalToBinary(int decimal) {
    long long binary = 0;
    int remainder, i = 1;
    while (decimal != 0) {
        remainder = decimal % 2;
        decimal /= 2;
        binary += remainder * i;
        i *= 10;
    }
    return binary;
}

int main() {
    long long octal;
    cout << "Enter an octal number: ";
    cin >> octal;
    
    int decimal = octalToDecimal(octal);
    long long binary = decimalToBinary(decimal);
    
    cout << "Octal: " << octal << " = Binary: " << binary << endl;
    
    return 0;
}
Output
Enter an octal number: 12
Octal: 12 = Binary: 1010

Understanding Convert Octal to Binary

This program teaches you how to convert an octal number directly to its binary equivalent in C++. This conversion can be done in two ways: either by converting octal→decimal→binary (two-step), or by converting each octal digit directly to 3 binary digits. Understanding this conversion helps you work efficiently with different number systems and understand the relationships between octal, decimal, and binary.

---

1. What This Program Does

The program converts an octal number to a binary number. For example:

Input octal: 12
Output binary: 1010
The program uses a two-step approach: first converts octal to decimal, then decimal to binary. This is straightforward and reuses the conversion algorithms we've already learned.

Example:

12 (octal) → 10 (decimal) → 1010 (binary)
17 (octal) → 15 (decimal) → 1111 (binary)

---

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 octal-to-decimal conversion.

---

3. Understanding the Two-Step Conversion

The program uses two helper functions:

Step 1: Octal to Decimal

Converts the octal number to decimal first.
Uses the algorithm: multiply each digit by 8^position and sum.

Step 2: Decimal to Binary

Converts the decimal result to binary.
Uses the algorithm: repeatedly divide by 2 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: octalToDecimal()

int octalToDecimal(long long octal) {

int decimal = 0, i = 0, remainder;

while (octal != 0) {

remainder = octal % 10;

octal /= 10;

decimal += remainder * pow(8, i);

++i;

}

}

This function:

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

How it works

(for octal = 12):

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

---

5. Function 2: decimalToBinary()

long long decimalToBinary(int decimal) {

long long binary = 0;

int remainder, i = 1;

while (decimal != 0) {

remainder = decimal % 2;

decimal /= 2;

binary += remainder * i;

i *= 10;

}

}

This function:

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

How it works

(for decimal = 10):

Repeatedly divides by 2.
Collects remainders: 10÷2=5 rem 0, 5÷2=2 rem 1, 2÷2=1 rem 0, 1÷2=0 rem 1.
Builds binary: 0 + 1×10 + 0×100 + 1×1000 = 1010

---

6. Main Function - Combining Both Steps

long long octal;

cout << "Enter an octal number: ";

cin >> octal;

int decimal = octalToDecimal(octal);

long long binary = decimalToBinary(decimal);

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

return 0;

}

int main() {

Process Flow

:

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

---

7. Direct Conversion Method (More Efficient)

Why Direct Conversion?

:

Two-step method works but is less efficient.
Direct conversion converts each octal digit to 3 binary digits.
More efficient: O(log n) vs O(log n) + O(log n).

How Direct Conversion Works

:

Each octal digit (0-7) converts directly to 3 binary digits.
Use conversion table for each digit.
Concatenate all binary groups.

Example

(octal 12 to binary):

Octal digit 2 → 010 (binary)
Octal digit 1 → 001 (binary)
Concatenate: 001010 → remove leading zeros → 1010 (binary)

Why 1 Octal Digit = 3 Binary Digits?

:

Octal is base-8, binary is base-2.
2³ = 8, so 1 octal digit needs 3 binary digits to represent 0-7.
This is the mathematical relationship between octal and binary.

---

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

Method 2: Direct Conversion

Converts each octal digit to 3 binary digits.
Uses conversion table: 0→000, 1→001, 2→010, ..., 7→111.
More efficient than two-step method.

Method 3: Using bitset

Uses bitset for binary representation.
Converts each digit and combines.
#include <bitset>
// Convert each octal digit to binary using bitset
// Concatenate results

Method 4: Using String Manipulation

Converts octal to string.
Processes each character (octal digit).
Converts each to 3-digit binary string.
Concatenates all binary strings.

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:

Octal: 12 = Binary: 1010

This clearly shows the conversion from octal to binary.

---

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

10. Understanding Octal-Binary Relationship

Why 1 Octal Digit = 3 Binary Digits?

:

Octal is base-8, binary is base-2.
2³ = 8, so 1 octal digit can represent 0-7, which requires 3 binary digits.
This makes direct conversion straightforward.

Conversion Table

(octal to 3-bit binary):

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

Direct Conversion Example

:

Octal: 1 2
Binary: 001 010
Result: 001010 → remove leading zeros → 1010 (binary)

---

11. When to Use Each Method

-

Two-Step Conversion

: Best for learning - clear, reuses known algorithms.

-

Direct Conversion

: Best for efficiency - faster, converts 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

Leading Zeros in Binary

:

When converting each octal digit, pad to 3 binary digits.
Example: 2 (octal) → 010 (binary), not 10.
Final result may have leading zeros that can be removed.

Large Octal Numbers

:

Very large octal 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 octal input contains only digits 0-7.
This program assumes valid octal input.
Invalid digits (8-9) would produce incorrect results.

---

13. return 0;

This ends the program successfully.

---

Summary

Octal-to-binary conversion can be done in two steps: octal→decimal→binary.
Two-step method is simple and reuses existing conversion algorithms.
Direct conversion converts each octal digit to 3 binary digits for efficiency.
1 octal digit = 3 binary digits (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 octal, decimal, and binary, 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 Octal to Binary

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