Convert Octal to Decimal

Convert Octal to Decimal in C++ (6 Programs)

IntermediateTopic: Advanced Number Programs
Back

C++ Convert Octal to Decimal 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 main() {
    long long octal;
    int decimal = 0, i = 0, remainder;
    
    cout << "Enter an octal number: ";
    cin >> octal;
    
    long long temp = octal;
    
    while (temp != 0) {
        remainder = temp % 10;
        temp /= 10;
        decimal += remainder * pow(8, i);
        ++i;
    }
    
    cout << "Octal: " << octal << " = Decimal: " << decimal << endl;
    
    return 0;
}
Output
Enter an octal number: 12
Octal: 12 = Decimal: 10

Understanding Convert Octal to Decimal

This program teaches you how to convert an octal number to its decimal (base-10) equivalent in C++. Octal is a base-8 number system that uses digits 0-7. While less common than binary or decimal, octal is still used in some computing contexts, especially for file permissions in Unix/Linux systems. Understanding octal conversion helps you work with different number systems.

---

1. What This Program Does

The program converts an octal number (base-8) to a decimal number (base-10). For example:

Input octal: 12
Output decimal: 10

Octal numbers use digits 0-7, while decimal numbers use digits 0-9. The conversion involves multiplying each octal digit by 8 raised to its position power and summing the results.

Example:

12 (octal) = 1×8¹ + 2×8⁰ = 8 + 2 = 10 (decimal)
145 (octal) = 1×8² + 4×8¹ + 5×8⁰ = 64 + 32 + 5 = 101 (decimal)

---

2. Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides pow() function for calculating powers.
Essential for computing 8 raised to various powers.

---

3. Declaring Variables

The program declares:

long long octal;

int decimal = 0, i = 0, remainder;

octal stores the octal number entered by the user (long long for large octal numbers).
decimal stores the converted decimal result (initialized to 0).
i tracks the position/index of the current digit (starts at 0 for rightmost digit).
remainder stores the last digit extracted in each iteration.

---

4. Taking Input From the User

The program asks:

cin >> octal;

The user enters an octal number, for example: 12

We also create a temporary copy:

long long temp = octal;

This is important because we'll modify the number during conversion, and we need the original for display.

---

cout << "Enter an octal number: ";

5. The Conversion Algorithm

The core conversion uses a while loop:

while (temp != 0) {

remainder = temp % 10;

temp /= 10;

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

++i;

}

Let's break down how this works step-by-step for octal = 12:

Step 1:

remainder = 12 % 10 = 2 (extract rightmost digit)
temp = 12 / 10 = 1 (remove rightmost digit)
decimal = 0 + 2 × 8⁰ = 0 + 2 × 1 = 0 + 2 = 2
i = 1 (move to next position)

Step 2:

remainder = 1 % 10 = 1
temp = 1 / 10 = 0
decimal = 2 + 1 × 8¹ = 2 + 1 × 8 = 2 + 8 = 10
i = 2

Step 3:

temp is now 0, so the loop stops
Final result: decimal = 10

---

6. Understanding the Octal System

Positional Value System

:

In octal, each position represents a power of 8:

Rightmost position (position 0): 8⁰ = 1
Next position (position 1): 8¹ = 8
Next position (position 2): 8² = 64
Next position (position 3): 8³ = 512
And so on...

Valid Octal Digits

:

Octal uses only digits 0, 1, 2, 3, 4, 5, 6, 7.
Digits 8 and 9 are not valid in octal.
Example: 18 is not a valid octal number.

Reading Octal from Right to Left

:

Position 0 (rightmost): units place (8⁰ = 1)
Position 1: eights place (8¹ = 8)
Position 2: sixty-fours place (8² = 64)

Example Calculation

:

Octal 145:

Digit at position 0: 5 → contributes 5 × 1 = 5
Digit at position 1: 4 → contributes 4 × 8 = 32
Digit at position 2: 1 → contributes 1 × 64 = 64
Total: 5 + 32 + 64 = 101

---

7. Key Operations Explained

Modulo Operator (%)

:

temp % 10 extracts the rightmost digit.
Example: 12 % 10 = 2, 145 % 10 = 5

Integer Division (/)

:

temp / 10 removes the rightmost digit.
Example: 12 / 10 = 1, 145 / 10 = 14

Power Function (pow())

:

pow(8, i) calculates 8 raised to the power i.
Example: pow(8, 0) = 1, pow(8, 1) = 8, pow(8, 2) = 64

Accumulation

:

decimal += remainder * pow(8, i) adds the contribution of current digit.
Each digit's contribution is: digit × 8^position

---

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

Method 2: Using String

string octalStr = to_string(octal);

int decimal = 0;

for (int i = 0; i < octalStr.length(); i++) {

int digit = octalStr[i] - '0';

decimal += digit * pow(8, octalStr.length() - 1 - i);

}

Converts octal to string.
Processes each character from left to right.

Method 3: Using Recursion

int octalToDecimal(long long octal, int i = 0) {

if (octal == 0) return 0;

}

Recursive approach - function calls itself.
Base case: when octal becomes 0.
    return (octal % 10) * pow(8, i) + octalToDecimal(octal / 10, i + 1);

Method 4: Using stoi() with Base 8

string octalStr = to_string(octal);

int decimal = stoi(octalStr, nullptr, 8);

stoi() can convert string to integer with specified base.
Base 8 means octal conversion.
Simplest method but requires string conversion.

Method 5: Manual Calculation

Similar to pow() method but calculates powers manually.
Uses multiplication instead of pow() function.

Method 6: Using Functions

Encapsulates conversion in a separate function.
Makes code more modular and reusable.

---

9. Displaying the Result

The program prints:

Output:

Octal: 12 = Decimal: 10

This clearly shows both the original octal number and its decimal equivalent.

---

cout << "Octal: " << octal << " = Decimal: " << decimal << endl;

10. Understanding Number Bases

Decimal (Base-10)

:

Uses digits 0-9.
Each position is a power of 10.
Example: 123 = 1×10² + 2×10¹ + 3×10⁰

Octal (Base-8)

:

Uses digits 0-7.
Each position is a power of 8.
Example: 145 = 1×8² + 4×8¹ + 5×8⁰

Binary (Base-2)

:

Uses digits 0-1.
Each position is a power of 2.

Why Octal?

:

Historically used in computing (easier to read than binary).
Still used for file permissions in Unix/Linux (e.g., chmod 755).
Groups of 3 binary digits correspond to 1 octal digit.

---

11. Common Octal Values

Powers of 8

:

8⁰ = 1
8¹ = 8
8² = 64
8³ = 512
8⁴ = 4096

Common Octal to Decimal

:

0 (octal) = 0 (decimal)
1 (octal) = 1 (decimal)
7 (octal) = 7 (decimal)
10 (octal) = 8 (decimal)
12 (octal) = 10 (decimal)
20 (octal) = 16 (decimal)
77 (octal) = 63 (decimal)
100 (octal) = 64 (decimal)

---

12. When to Use Each Method

-

pow() Method

: Good for learning - clear and straightforward.

-

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.

-

Manual Calculation

: More efficient - avoids pow() function calls.

-

Functions

: Best for code organization - modular and reusable.

Best Practice

: Use stoi() for simplicity, or manual calculation for efficiency.

---

13. Important Considerations

Valid Octal Digits

:

Octal numbers can only contain digits 0-7.
This program doesn't validate input - assumes valid octal.
Invalid digits (8-9) would produce incorrect results.

Large Octal Numbers

:

long long is used to handle large octal numbers.
Very large octal numbers may still cause overflow.
Consider using string for very large numbers.

File Permissions

:

Unix/Linux file permissions use octal notation.
Example: 755 (octal) means rwxr-xr-x permissions.
Understanding octal helps with system administration.

---

14. return 0;

This ends the program successfully.

---

Summary

Octal-to-decimal conversion multiplies each digit by 8^position and sums results.
The algorithm processes digits from right to left (least significant first).
Octal uses base-8 (digits 0-7), decimal uses base-10 (digits 0-9).
Understanding octal is useful for file permissions and historical computing contexts.
Multiple methods exist: pow(), string, recursion, stoi(), manual calculation, functions.
Choose method based on needs: learning vs. efficiency vs. simplicity.
Octal is less common than binary/decimal but still has practical applications.

This program is fundamental for beginners learning different number systems, understanding positional notation, and preparing for system administration tasks and advanced computer science topics 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 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.

Table of Contents