Convert Octal to Decimal in C++
Convert an octal number like 12 into decimal in C++ by multiplying each digit by a power of 8. Explains octal place values and why digits 8 and 9 are invalid.
1#include <iostream>2#include <cmath>3using namespace std;45int main() {6 long long octal;7 int decimal = 0, i = 0, remainder;89 cout << "Enter an octal number: ";10 cin >> octal;1112 long long temp = octal;1314 while (temp != 0) {15 remainder = temp % 10;16 temp /= 10;17 decimal += remainder * pow(8, i);18 ++i;19 }2021 cout << "Octal: " << octal << " = Decimal: " << decimal << endl;2223 return 0;24}
Sample Output
Octal: 12 = Decimal: 10
1 × 8 + 2.
In this one
- How base 8 place values work
- How to read octal digits with % 10
- How pow(8, i) weights each digit
- Why octal digits stop at 7
How it works
- 1
Read 12.
- 2
Last digit 2 × 8^0 = 2.
- 3
Next digit 1 × 8^1 = 8.
- 4
Sum = 10.
- 5
Print it.
Edge cases
Digit 8 or 9
Enter 19.
The program accepts it and computes 9 + 8 = 17, but 19 is not a valid octal number. Check each digit is 0–7.
Leading zero in C++ source
Write int x = 012; in code.
A leading zero makes a C++ literal octal, so x is 10. cin does not do this by default; it reads 012 as twelve.
What's going on
The program converts an octal number (base-8) to a decimal number (base-10). For example:
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:
Header Files Used
Declaring Variables
The program declares:
long long octal;
int decimal = 0, i = 0, remainder;
Taking Input From the User
The program asks:
cout << "Enter an octal number: ";
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.
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:
Step 2:
Step 3:
Understanding the Octal System
Positional Value System:
In octal, each position represents a power of 8:
Valid Octal Digits:
Reading Octal from Right to Left:
Example Calculation:
Octal 145:
Key Operations Explained
Modulo Operator (%):
Integer Division (/):
Power Function (pow()):
Accumulation:
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);
}
Method 3: Using Recursion
int octalToDecimal(long long octal, int i = 0) {
if (octal == 0) return 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);
Method 5: Manual Calculation
Method 6: Using Functions
Displaying the Result
The program prints:
cout << "Octal: " << octal << " = Decimal: " << decimal << endl;
Output:
Octal: 12 = Decimal: 10
This clearly shows both the original octal number and its decimal equivalent.
Understanding Number Bases
Decimal (Base-10):
Octal (Base-8):
Binary (Base-2):
Why Octal?:
Common Octal Values
Powers of 8:
Common Octal to Decimal:
When to Use Each Method
Best Practice: Use stoi() for simplicity, or manual calculation for efficiency.
Important Considerations
Valid Octal Digits:
Large Octal Numbers:
File Permissions:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Octal 17 in decimal?
Pick an answer to check it.
Question 2
Which is not a valid octal digit?
Pick an answer to check it.
Questions people ask
Where is octal used?
Unix file permissions like 755 are octal.
Is there a library way?
stoi("12", nullptr, 8) returns 10.
How do I print a number in octal?
cout << oct << 10; prints 12.