Convert Octal to Decimal in C++

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

IntermediateAdvanced Number ProgramsExample 7 of 13
convert-octal-to-decimal.cpp
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5int main() {
6 long long octal;
7 int decimal = 0, i = 0, remainder;
8
9 cout << "Enter an octal number: ";
10 cin >> octal;
11
12 long long temp = octal;
13
14 while (temp != 0) {
15 remainder = temp % 10;
16 temp /= 10;
17 decimal += remainder * pow(8, i);
18 ++i;
19 }
20
21 cout << "Octal: " << octal << " = Decimal: " << decimal << endl;
22
23 return 0;
24}

Output

Enter an octal number: 12
Octal: 12 = Decimal: 10

What's going on

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)

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.

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.

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.

1cout << "Enter an octal number: ";

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

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

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

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.
1return (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.

Displaying the Result

The program prints:

Output:

Octal: 12 = Decimal: 10

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

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

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.

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)

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.

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.

return 0;

This ends the program successfully.