How to convert Decimal to octal in Python?

A Decimal is a number that contains a point in it. In Python, decimals are also known as floating points as well. While Octal is a different method for counting. Sometimes, you might need to convert decimal to octal in Python without using any built-in methods in Python. In this short article, we will discuss how we can convert decimals to octal in Python using various methods.

How to convert Decimal to octal in Python?

A decimal number is also known as a floating number in Python. Decimal numbers start from 0 and go on. While octal numbers are another ways of counting.

Decimals-to-octal-in-Python

Let us go through various methods to convert the decimal to octal in Python.

Method-1: Using the built-in method

The oct() method in Python is used to convert a decimal number into an octal. Let us first convert the decimal to octal using python by taking an example.

# decimal number 
a = 10

# decimal to octal in python
o = oct(a)

# print
print(o)

Output:

0o12

Here is what the above code is doing:

  1. a = 10 – assigns the decimal number 10 to the variable a.
  2. o = oct(a) – calls the oct() function with the argument a to convert the decimal number to its octal representation. The result is stored in the variable o.
  3. print(o) – prints the octal representation of the decimal number 10, which is “0o12” in Python.

Method-2: Using While loop

We can use the while loop to convert decimal to octal in Python. A while loop is a loop that iterates through the object untill the condition is not False. Let us now learn how we can use the while loop to convert the decimal to octal and then we will explain the code.

# variables
decimal = 10
octal = 0
ctr = 0
temp = decimal  
 
#computing octal using while loop
while(temp > 0):
    octal += ((temp%8)*(10**ctr))  
    temp = int(temp/8)             
    ctr += 1
       
print(octal)

Output:

12

Let us now go through the code step by step to understand how it actually works:

  1. decimal = 10: This line initializes a decimal variable with a value of 10. You can replace this with any decimal number you want to convert to octal.
  2. octal = 0: This line initializes an octal variable with a value of 0. This variable will be used to store the octal representation of the decimal number.
  3. ctr = 0: This line initializes a counter variable with a value of 0. This variable will be used to keep track of the position of each digit in the octal number.
  4. temp = decimal: This line initializes a temporary variable with the same value as the decimal variable. This variable will be used to perform the conversion.
  5. while(temp > 0):: This line starts a while loop that will continue as long as the temporary variable is greater than 0.
  6. octal += ((temp%8)*(10**ctr)): This line calculates the value of the current digit in the octal number and adds it to the octal variable. The calculation is done by getting the remainder of the temporary variable divided by 8 (which gives the current octal digit), multiplying it by 10 raised to the power of the counter variable (which gives the position of the digit), and adding it to the current value of the octal variable.
  7. temp = int(temp/8): This line updates the value of the temporary variable by dividing it by 8 and converting the result to an integer (to get the quotient of the division).
  8. ctr += 1: This line updates the value of the counter variable by incrementing it by 1.
  9. print(octal): This line prints the final value of the octal variable, which represents the octal representation of the decimal number.

Summary

In this short article, we discussed how we can convert decimal to octal using Python. We covered two methods and explained each method deeply.

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top