Python Program to Convert Octal to Decimal Number
You can convert an octal number to decimal in Python using
decimal_number = int(octal_string, 8), where octal_string is the octal input as a string.Examples
Input7
Output7
Input10
Output8
Input377
Output255
How to Think About It
To convert octal to decimal, think of each digit in the octal number as a place value multiplied by powers of 8. Starting from the rightmost digit, multiply it by 8 raised to the position index (starting at 0) and add all these values together to get the decimal number.
Algorithm
1
Get the octal number as a string input.2
Initialize a decimal result to zero.3
For each digit in the octal string from right to left, convert it to an integer.4
Multiply the digit by 8 raised to the power of its position index.5
Add the result to the decimal total.6
Return or print the decimal total.Code
python
octal_str = input("Enter an octal number: ") decimal_num = int(octal_str, 8) print(f"Decimal number: {decimal_num}")
Output
Enter an octal number: 377
Decimal number: 255
Dry Run
Let's trace the input '377' through the code
1
Input octal string
octal_str = '377'
2
Convert octal to decimal
decimal_num = int('377', 8) which equals 3*8^2 + 7*8^1 + 7*8^0 = 192 + 56 + 7 = 255
3
Print result
Output: Decimal number: 255
| Digit | Position (right to left) | Calculation | Running Total |
|---|---|---|---|
| 7 | 0 | 7 * 8^0 = 7 | 7 |
| 7 | 1 | 7 * 8^1 = 56 | 63 |
| 3 | 2 | 3 * 8^2 = 192 | 255 |
Why This Works
Step 1: Using int() with base 8
The int() function can convert a string in any base to decimal by specifying the base as 8 for octal.
Step 2: Octal place values
Each digit in octal represents a power of 8, starting from the rightmost digit at power 0.
Step 3: Summing values
Multiplying each digit by its place value and adding all results gives the decimal equivalent.
Alternative Approaches
Manual calculation using loop
python
octal_str = input("Enter octal number: ") decimal_num = 0 power = 0 for digit in reversed(octal_str): decimal_num += int(digit) * (8 ** power) power += 1 print(f"Decimal number: {decimal_num}")
This method shows the step-by-step calculation but is longer than using int() with base 8.
Using recursion
python
def octal_to_decimal(octal_str): if len(octal_str) == 0: return 0 return octal_to_decimal(octal_str[:-1]) * 8 + int(octal_str[-1]) num = input("Enter octal number: ") print(f"Decimal number: {octal_to_decimal(num)}")
This recursive approach is elegant but less efficient for very long numbers.
Complexity: O(n) time, O(1) space
Time Complexity
The conversion scans each digit once, so time grows linearly with the number of digits.
Space Complexity
Only a few variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
Using Python's built-in int() with base 8 is fastest and simplest compared to manual or recursive methods.
| Approach | Time | Space | Best For |
|---|---|---|---|
| int() with base 8 | O(n) | O(1) | Simple and fast conversion |
| Manual loop calculation | O(n) | O(1) | Learning and understanding conversion |
| Recursive method | O(n) | O(n) | Elegant code but uses more stack space |
Always input the octal number as a string to avoid confusion with Python's default number formats.
Trying to convert octal numbers directly as integers without specifying base 8 causes wrong results.