In this post, we will learn how to convert decimal to octal in Python with detailed explanation and example. But before we jump into it, let’s start with the basics.

Decimal numbers are a number system that consists of 10 digits from 0 to 9. Using these numbers, we can form all other numbers, and basically, we use decimal numbers in our daily lives. On the other hand, octal numbers are a number system that consists of 8 digits from 0 to 7. Using these numbers, we can represent all other numbers, and they are commonly used in computer science.

Now let’s understand how we can convert a decimal number into an octal number.

## Convert Decimal to Octal Number

Suppose you want to convert 100 to an octal number. The process involves dividing 100 by 8 and storing its remainder (100 % 8). Then, take the quotient (12) and divide it by 8, storing the remainder again. Repeat this process until you reach zero. After that, reverse the stored remainders to obtain the octal conversion of the decimal number.

For a better understanding please refer to the figure below.

## Algorithm

1. Take input from the user (`num`).
2. Create an empty list to store octal numbers (`octal_num = []`).
3. `while num != 0` do
`rem = num %8octal_num.append(rem)num = num // 8`
4. Reverse a list (`octal_num`) using slicing (`[::-1]`).
5. At the end print the list (`octal_num`).

From the above algorithm and explanation, I hope you understand how to implement a Python program to convert decimal to octal numbers but before writing the program few programming concepts you have to know:

### Source Code

``````num = int(input("Enter a number: "))

# empty list to store octal numbers
octal_num = []

while num != 0:
rem = num % 8
octal_num.append(rem)
num = num // 8

# reverse a list
octal_num = octal_num[::-1]

print("Octal of given number is ",end='')
for i in octal_num:
print(i,end="")``````

### Output-1

``````Enter a number: 100
Octal of given number is 144``````

### Output-2

``````Enter a number: 10
Octal of given number is 12``````

## Convert Decimal to Octal in Python Using Built-in function

There is also a built-in function called `oct()` in Python to convert decimal to octal. Let’s see how with the help of example.

### Source Code

``````num = int(input("Enter a number: "))

octal = oct(num)
print(f"Octal of {num} is {octal}")``````

### Output

``````Enter a number: 100
Octal of 100 is 0o144``````

Here, first two character `0o` represents that the given expression is in octal, and the remaining characters is an actual octal representation (`144`) of the given number.

This is all about how we convert decimals to octal numbers in Python. The overall conclusion is that there are two ways to convert decimal to octal: one is the conventional method, and the other involves using the built-in function in Python. Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.