# Python Decimal to Octal Conversion

By | July 21, 2019

Python Decimal to Octal Conversion – This tutorial explains the logic of decimal number to octal number conversion process. It also explains the Python code to convert a dcimal nuber into octal number.

How to Convert a decimal Number to Octal Number

1. We divide the decimal number by base 8 and get the quiotient and remainder.  Example: divide 119 by 8, quotient1 is 17 and remainder1 is 7.
2. Then we divide the quotient by base 8 and get the quotient and remainder. The Example continues from step 1: divide quotient1 by 8, quotient2 is 1 and remainder2 is 6.
3. This process continues until the quotient bcomes zero. The example continues from step 2: divide quotient2 by 8, now quotient3 is 0 (zero), so we stop, and remainder3 is 1.
4. Now we write all remainders in reverse order to get the required binary number. The example continus from step 3. We will write remainder3 = 1,remainder2=6,remainder1=7 (in reverse order) so that we will get the required binary number 167.

## The Source code of python Program Decimal to Octal Number Conversion

```# This Python script / program is used to input
# a decimal number and convert into octal number
# using a while loop
# author: www.EasyCodeBook.com (c)

num = int(input('Enter a Decimal number to Convert into Octal:'))
q=num
octal=0
i=0

while q!=0:
rem = q%8
octal = octal + rem * (10**i)
q = q // 8
i+=1

print('Octal Number=',octal)

```
```The output of python program for decimal number to octal conversion is as follows:
Enter a Decimal number to Convert into Octal:119
Octal Number= 167
```