How To Convert Decimal to Octal In Python

A decimal number has a base of 10. The number of digits required to express a numeric value depends on the base value of the underlying number system. For example, the decimal number system employs 10 digits 0 to 9 to represent any numeric value, while the octal number system requires 8 digits from 0 to 7.

 Example: Following numbers are represented in the decimal numbers whose base is 10.

32, 128, 150, 845, 525, 52652

 Example:Following numbers are represented in the decimal numbers whose base is  8.

10, 22, 1214, 20563, 754261

Following table represents the decimal number and its equivalent octal number.

Decimal NumberEquivalent Octal Number
(24)10(30)8
(64)10(100)8
(452)10(704)8

Why Is There A Need To Convert Decimal to Octal In Python

Octal number is used at several places in the computer system for data operations and processing. Though there are many reasons to convert decimal numbers into octal numbers.

  • File Processing:  Octal number is often used to represent file permissions in UNIX, the source code for Linux, mac OS, and android, among other operating systems.
  • Digital Display: Digital displays, which lack symbol support as well, also utilize octal number. Since it is less error-prone than attempting to achieve the same thing in hex, aircraft still employ octal (4-digit / 12-bit) transponder codes for communication with ATC.    
  • System Code Processing: Due to the bit sizes (the number of bits that could be processed at once) of the CPUs that were in use at the time, which were 26 and 36 bits, it was regarded as the appropriate shorthand for binary. Octal was no longer used once Processors changed to 32- and 64-bit bit sizes because, although both can still be divided by 8, they can also be divided by 16 for far greater efficiency.
  • Programming Language: Most programming languages support the integer data formats in different number systems. For the production of integer values in the binary number system, we can use octal numbers.  In memory storage, all integer values are represented by setting the values of binary digits.

Approaches to Convert Decimal to Octal

Number of approaches may be used to convert a decimal number to octal. There are some approaches listed below. 

  • Approach-1: Using Oct() Built-in Function
  • Approach-2: Using Recursion
  • Approach-3: Using While Loop
  • Approach-4: Using Modulus (%) Operator

Lets dive in details for each approach.

Approach-1: Using Oct() Built-in Function

Python language allows us to convert decimal numbers to octal numbers using an in-built function named Oct(). The Oct() function takes a number as an argument and returns an equivalent function. Following algorithm describes the steps involved for the conversion of decimal into its equivalent octal number.

Algorithm:

Step-1: Taking decimal number as an input from the user and store the value of given decimal number into a variable.

Step-2: Call Oct() function and pass decimal number as an argument.

Step-3: Octal number is returned by the Oct() function.

To demonstrates this approach, a program is created which code is given below:

Code:

# Python program to demonstrate conversion of Decimal to Octal
decimal=int(input('Enter a decimal number: '))       # It ask decimal number as an input from the users
octal=oct(decimal)                             # Oct() function convert given decimal to octal number
print('Octal number equivalent to given decimal is: ', octal)  # It display resulted octal number

Output:

Enter a decimal number: 512
Octal number equivalent to given decimal is:  0o1000

Code Explanation:

  • The decimal number is asked from the user and stores its value in the Decimal variable.
  • Oct() function is called with the argument Decimal number.
  • The result of the function  is stored in the variable  named ‘Octal’ that is an octal number.
  • The octal number is displayed using print function 

Approach-2: Using Recursion

Recursion is another approach which may be used to convert decimal numbers to octal numbers. This method is accomplished through recursive calls of a user defined function.   An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Recursive function named Decimal_to_Octal is created which takes Decimal/8 as an argument.

Step-3: Print remainder. Function  is called recursive until Decimal becomes 0.

Code:

# Python program to demonstrate conversion of Decimal to Octal using recursive function
decimal=int(input('Enter a decimal number: '))  # It ask decimal number as an input from the users
def Decimal_to_Octal(decimal):     # Recursive Function named Decimal_to_Octal() is defined
  if decimal>0:                     # Check whether decimal is 0 or not 
    Decimal_to_Octal(int(decimal/8)) #It recursively call Decimal_to_Octal() function
    print(decimal%8,end='')         # It display remainder
Decimal_to_Octal(decimal)      # Decimal_to_Octal() function is called

Output:

Enter a decimal number: 1024
2000

Code Explanation:

  • Ask decimal number from the user
  • Recursive function named Decimal_to_Octal is called which takes Decimal number as an argument.
  • Decimal_to_Octal() function is called recursively until the decimal is 0 and displays the remainder.

Approach-3: Using  While Loop

We can convert decimal to octal using a while loop, division (/) and multiplication (*) operators. This approach is defined in the following algorithm.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using division operator(/)  and multiply quotient with 8 using multiplication operator.

Step-3: Remainder is computed by the difference of Decimal number and (quotient *8). 

Step-4 Store each remainder in the numeric list.

Step-5: Repeat step 2, 3,4 until input number is 0

Step-6: Display octal list in reverse order.

Code:

# Python program to demonstrate conversion of Decimal to Octal using While Loop 
octal=[]
decimal=int(input('Enter a decimal number: '))       # It ask decimal number as an input from the users
while decimal>0:
  qt = int(decimal / 8)      # Divides decimal by 8 using simple division operator  
  NR = qt * 8;
  NumRemain = decimal - NR
  octal.append(NumRemain)
  decimal = int(decimal / 8)
octal.reverse()
for i in octal:
  print(i,end='')

Output:

Enter a decimal number: 650
1212

Code Explanation:

  • Ask a decimal number from the user.
  • Divide decimal number with 8 and compute remainder in octal numeric list  using division and multiplication operators. 
  • After that the value of the ‘octal’ list is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Approach-4: Using Modulus(%) Operator

A decimal number may be converted to octal number using a modulus(%) operator. In this approach, a decimal number is asked from the user and an octal number is obtained. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Taking decimal number as an input from the user and store the value of given decimal number into a variable.

Step-2: Divide the given decimal number by 8 then record the resultant number and remainder value.

Step-3: Now, iteratively divide the resultant value of the previous step (step 2) until the new resultant number is 0. Also record the reminder values in each step.

Step-4: Now, print the reverse of all reminder values as the output equivalent octal number.

Code:

# Python program to demonstrate conversion of Decimal to Octal using Modulus(%) Operator 
octal=[]
decimal=int(input('Enter a decimal number: '))       # It ask decimal number as an input from the users
while decimal>0:
  qt = decimal % 8      # Divides decimal by 8 using simple modulus operator  
  octal.append(qt)
  decimal = int(decimal / 8)
octal.reverse()
for i in octal:
  print(i,end='')

Output:

Enter a decimal number: 1566
3036

Code Explanation:

  • The decimal number is asked from the user and stores its value in the ‘Decimal’ variable.
  • Using a loop, the given decimal number is iteratively divided until its value becomes 0.
  • The remainder is stored iteratively in a list named ‘Octal’ that is an octal number.
  • After that the value of the ‘Octal’ list is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Best Approach- Format Specifier (%)

The best approach  is to convert  a decimal number to octal number using an in-built function named oct() . It is the best approach because:

  • It is  a short and very convenient way to decimal to octal number.
  • It used to convert any input number to octal number such as binary, hexadecimal and decimal.
  • It offers a short length of code.

Sample problems related to convert decimal to hexadecimal

Sample Problem-1: Using Approach-1

Problem Definition: Implement a converter which converts a decimal number into octal representation. Therefore, create an algorithm and python code to demonstrate conversion of decimal to octal number. 

Solution: An algorithm for this problem is represented in the following steps.

 Step-1: Taking decimal number as an input from the user and store the value of given decimal number into a variable.

Step-2: Call Oct() function and pass decimal number as an argument.

Step-3: Octal number is returned by the Oct() function.

Code:

# Python program to demonstrate conversion of Decimal to Octal using approach-1
decimal=int(input('Enter a decimal number: '))       # It ask decimal number as an input from the users
octal=oct(decimal)                             # Oct() function convert given decimal to octal number
print('Octal number equivalent to given decimal is: ', octal)  # It display resulted octal number

Output:

Enter a decimal number: 512
Octal number equivalent to given decimal is:  0o1000

Code Explanation:

  • The decimal number is asked from the user and stores its value in the Decimal variable.
  • Oct() function is called with the argument Decimal number.
  • The result of the function  is stored in the variable  named ‘Octal’ that is an octal number.
  • The octal number is displayed using print function 

Sample Problem-2: Using Approach-2

Problem Definition: Consider a computer programmer who wants to process user inputs into octal representation. Therefore create an algorithm and python code to demonstrate conversion of decimal numbers to octal representations.

Solution: An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Recursive function named Decimal_to_Octal is created which takes Decimal/8 as an argument.

Step-3: Print remainder i.e. Decimal%8. Function  is called recursive until Decimal becomes 0.

Code:

# Python program to demonstrate conversion of Decimal to Octal using approach-2
decimal=int(input('Enter a decimal number: '))  # It ask decimal number as an input from the users
def Decimal_to_Octal(decimal):     # Recursive Function named Decimal_to_Octal() is defined
  if decimal>0:                     # Check whether decimal is 0 or not 
    Decimal_to_Octal(int(decimal/8)) #It recursively call Decimal_to_Octal() function
    print(decimal%8,end='')         # It display remainder
Decimal_to_Octal(decimal)      # Decimal_to_Octal() function is called

Output:

Enter a decimal number: 10
12

Code Explanation:

  • Ask decimal number from the user
  • Recursive function named Decimal_to_Octal is called which takes Decimal number as an argument.
  • Decimal_to_Octal() function is called recursively until the decimal is 0 and displays the remainder.

Sample Problem-3:Using Approach-3

Problem Definition: Consider a  company that wants to develop a digital calculator whose motive is to implement digital number system conversion. Digital calculator is able to convert one number format to another. For example to convert a decimal to binary, octal and hexadecimal as well as vice versa.  Therefore create an algorithm and  python code to demonstrate how to convert a decimal number to octal number.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using division operator(/)  and multiply quotient with 8 using multiplication operator.

Step-3: Remainder is computed by the difference of Decimal number and (quotient *8). 

Step-4 Store each remainder in the numeric list.

Step-5: Repeat step 2, 3,4 until input number is 0

Step-6: Display octal list in reverse order.

Code:

# Python program to demonstrate conversion of Decimal to Octal using While Loop 
octal=[]
decimal=int(input('Enter a decimal number: '))       # It ask decimal number as an input from the users
while decimal>0:
  qt = int(decimal / 8)      # Divides decimal by 8 using simple division operator  
  NR = qt * 8;
  NumRemain = decimal - NR
  octal.append(NumRemain)
  decimal = int(decimal / 8)
octal.reverse()
for i in octal:
  print(i,end='')

Output:

Enter a decimal number: 65
101

Code Explanation:

  • Ask a decimal number from the user.
  • Divide decimal number with 8 and compute remainder in octal numeric list  using division and multiplication operators. 
  • After that the value of the ‘octal’ list is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number.

Sample Problem-4: Using Approach-4

Problem Definition: Consider a company that wants to implement a digital display where it takes input from the user in decimal form and converts into octal representation i.e. 0-7. Therefore, create an algorithm and  python code to demonstrate these conversions.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Taking decimal number as an input from the user and store the value of given decimal number into a variable.

Step-2: Divide the given decimal number by 8 then record the resultant number and remainder value.

Step-3: Now, iteratively divide the resultant value of the previous step (step 2) until the new resultant number is 0. Also record the reminder values in each step.

Step-4: Now, print the reverse of all reminder values as the output equivalent octal number.

Code:

# Python program to demonstrate conversion of Decimal to Octal using Modulus(%) Operator 
octal=[]
decimal=int(input('Enter a decimal number: '))       # It ask decimal number as an input from the users
while decimal>0:
  qt = decimal % 8      # Divides decimal by 8 using simple modulus operator  
  octal.append(qt)
  decimal = int(decimal / 8)
octal.reverse()
for i in octal:
  print(i,end='')

Output:

Enter a decimal number: 15
17

Code Explanation:

  • The decimal number is asked from the user and stores its value in the ‘Decimal’ variable.
  • Using a loop, the given decimal number is iteratively divided until its value becomes 0.
  • The remainder is stored iteratively in a list named ‘Octal’ that is an octal number.
  • After that the value of the ‘Octal’ list is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Conclusion

Python supports many number systems representations and their conversions using inbuilt functions and other methods. A decimal number may be converted into different other numbers such as binary, octal, hexadecimal and vice versa.

In this article, many approaches to convert a decimal number to octal number were presented with an algorithm, python code, output and its explanation.

A built-in function named oct() approach is considered as the best approach because it offers a shortened and convenient way.