0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Decimal to Octal Number

You can convert a decimal number to octal in Python using oct(decimal_number) which returns a string with prefix '0o', or by using a custom function that divides the number by 8 repeatedly and collects remainders.
📋

Examples

Input8
Output0o10
Input65
Output0o101
Input0
Output0o0
🧠

How to Think About It

To convert a decimal number to octal, repeatedly divide the number by 8 and keep track of the remainders. These remainders, read in reverse order, form the octal number. Alternatively, Python's built-in oct() function can be used to get the octal representation directly.
📐

Algorithm

1
Get the decimal number as input.
2
If the number is zero, return '0'.
3
Initialize an empty string to store octal digits.
4
While the number is greater than zero, divide it by 8 and record the remainder.
5
Prepend the remainder to the octal string.
6
Update the number to the quotient of the division.
7
Return the octal string.
💻

Code

python
def decimal_to_octal(num):
    if num == 0:
        return '0'
    octal = ''
    while num > 0:
        octal = str(num % 8) + octal
        num //= 8
    return octal

number = 65
print(f"Octal of {number} is {decimal_to_octal(number)}")
Output
Octal of 65 is 101
🔍

Dry Run

Let's trace the decimal number 65 through the code to convert it to octal.

1

Start with number 65

num = 65, octal = ''

2

Divide 65 by 8

65 % 8 = 1 (remainder), 65 // 8 = 8 (quotient), octal = '1'

3

Divide 8 by 8

8 % 8 = 0 (remainder), 8 // 8 = 1 (quotient), octal = '01'

4

Divide 1 by 8

1 % 8 = 1 (remainder), 1 // 8 = 0 (quotient), octal = '101'

5

Number is now 0, stop

Final octal string = '101'

num before divisionremainder (num % 8)quotient (num // 8)octal string
65181
80101
110101
💡

Why This Works

Step 1: Using modulo to find remainder

Each time we divide the number by 8, the remainder gives the next octal digit from right to left.

Step 2: Building the octal string

We add each remainder to the front of the string to build the octal number in correct order.

Step 3: Stopping condition

When the number becomes zero after division, all octal digits have been found.

🔄

Alternative Approaches

Using built-in oct() function
python
number = 65
print(oct(number))
This is the simplest and fastest way but returns a string with '0o' prefix.
Using recursion
python
def decimal_to_octal_recursive(num):
    if num == 0:
        return ''
    return decimal_to_octal_recursive(num // 8) + str(num % 8)

number = 65
result = decimal_to_octal_recursive(number) or '0'
print(result)
This approach uses recursion to build the octal string but may be less efficient for large numbers.

Complexity: O(log n) time, O(log n) space

Time Complexity

The algorithm divides the number by 8 repeatedly, so the number of steps is proportional to the number of octal digits, which is logarithmic in base 8.

Space Complexity

The space used is for the output string storing octal digits, which grows with the number of digits, also logarithmic.

Which Approach is Fastest?

Using Python's built-in oct() is fastest and simplest, while manual methods offer learning value but are slower.

ApproachTimeSpaceBest For
Built-in oct()O(1)O(1)Quick and simple conversion
Iterative divisionO(log n)O(log n)Learning and manual control
Recursive divisionO(log n)O(log n)Understanding recursion
💡
Use Python's built-in oct() for quick conversion, but remove the '0o' prefix if you want only digits.
⚠️
Beginners often forget to reverse the order of remainders, resulting in incorrect octal numbers.