Python Decimal to Octal Conversion


In this article, we will learn how to convert a decimal to octal in Python using multiple different methods.

    Table of Contents

  1. Decimal and Octal Numbers
  2. Method 1: Using oct() functions
  3. Method 2: Using format() function
  4. Method 3: Custom function
  5. Method 4: Using recursion

Decimal and Octal Numbers

Decimal Number

+Decimal numbers are the numbers that we use in our day-to-day life. They are also called Base 10 numbers. The decimal number system has 10 digits from 0 to 9.

Octal Number

Octal numbers are the numbers that include digits from 0 to 7. They are also called Base 8 numbers.

Octal numbers are used in computer programming. They are used to represent the permissions of a file in Linux. For example, the permission of a file is 644. Here, 6 is the permission for the owner, 4 is the permission for the group and 4 is the permission for others.

Decimal to Octal Conversion

There could be multiple ways to convert a decimal number to an octal number but at the base level, we need to divide the decimal number by 8 and keep on dividing until we get a quotient of 0.

The remainder of each division is written in reverse order to get the octal number.

decimal to octal conversion
Decimal to Octal Conversion

1. Decimal to Octal in Python using oct()

The oct() is a built-in function in Python that can convert a decimal number to an octal number.

It takes a decimal number as an argument and returns the octal number.

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

octal = oct(num)
print(octal)

Output:

Enter a decimal number: 123
0o173

2. Decimal to Octal in Python using format()

The format() function is used to format the output of a number. But it can also be used to convert numbers from one base to another.

For this purpose, it uses format specifiers. The format specifier for octal number is o.

It can be used in different ways. Look at the following examples:

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

oct1 = format(num, 'o')
print(oct1)

oct2 = '{:o}'.format(num)
print(oct2)

oct3 = f'{num:o}'
print(oct3)

Output:

Enter a decimal number: 73
111
111
111

3. Decimal to Octal in Python using Custom Function

We have discussed above how we can convert decimal numbers to octal numbers by dividing the decimal number by 8.

Now let's create a function that takes a decimal number as an argument and returns the octal number.

Algorithm

  1. Take a decimal number as an argument.
  2. Initialize an empty string to store the octal number if you want to return a string or assign 0 to a variable if you want to return an integer.
  3. Execute a while loop that runs until the input number is greater than 0.
  4. Find the remainder of the input number by 8 and concatenate it to the string at the beginning or add it to the variable by multiplying it with a factor of 10.
  5. Divide the input number by 8 and assign it to the input number.
  6. Return the string or the variable.
  • Return integer
def dec2oct(num):
    # empty string to store the octal number
    octal = ''
    while num > 0:
        octal = str(num % 8) + octal
        num //= 8
    return octal

num = int(input("Enter a decimal number: "))
print(dec2oct(num))
def dec2oct(num):
    # define octal number and initialize it to 0
    octal = 0
    i = 1
    while num > 0:
        octal += (num % 8) * i
        num //= 8
        i *= 10
    return octal

num = int(input("Enter a decimal number: "))
print(dec2oct(num))

Output:

Enter a decimal number: 258
402

4. Decimal to Octal in Python using Recursion

In this program, we will use recursion to convert decimal numbers to octal numbers.

Algorithm

  1. Take a decimal number as an argument.
  2. Check if the input number is less than 8. If yes, return the input number.
  3. Otherwise, return the remainder of the input number by 8 concatenated with the function call with the input number divided by 8.
def dec2oct(num):
    if num < 8:
        return num
    else:
        return str(num % 8) + str(dec2oct(num // 8))

num = int(input("Enter a decimal number: "))
print(dec2oct(num))

Output:

Enter a decimal number: 81
121