Home » Python Program to Convert Decimal to Octal

Python Program to Convert Decimal to Octal

Learn about Python Program to Convert Decimal to Octal in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Python Program to Convert Decimal to Octal

The below program converts a given decimal value to an octal value.

Program:

# Python program to convert decimal to octal

# function
def DecimalOctal(num):
    octal = [0] * 100

    # counter for octal number array
    i = 0
    while (num != 0):
        octal[i] = num % 8
        num = int(num / 8)
        i += 1

    # print octal number array in reverse order
    for j in range(i - 1, -1, -1):
        print(octal[j], end='')

# inputs from user
num = int(input('Enter a decimal number: '))

# function call
print('Octal value: ', end='')
DecimalOctal(num)

Output:

Enter a decimal number: 56
Octal value: 70

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Python program to convert Binary to Decimal