Convert Octal to Decimal in C++
Convert Octal to Decimal in C++ (6 Programs).
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}
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:
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:
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:
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;
}
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);
Method 5: Manual Calculation
Method 6: Using Functions
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)
:
Octal (Base-8)
:
Binary (Base-2)
:
Why Octal?
:
Common Octal Values
Powers of 8
:
Common Octal to 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
:
Large Octal Numbers
:
File Permissions
:
return 0;
This ends the program successfully.