Convert decimal to octal in Python
In this post, you will learn how to convert decimal to octal using the Python programming language.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of the decimal number system are that it is easy-to-read, used by humans, and easy to manipulate. But there are some disadvantages, like wastage of space and time.
The octal number system has a base of eight, like 238, 1028, 418, etc., and uses the numbers from 0 to 7. The octal number system is widely used in computer application sectors and digital numbering systems. The main advantage of using Octal numbers is that it uses less digits than decimal and hexadecimal number systems. That's why it has fewer computations and less computational errors.
Convert decimal to octal in python
In the given Python program, we have used the while loop to convert from decimal to an octal. The standard mathematical approach to converting a decimal to an octal is to divide the number by 8 until it reduces to zero. The sequence of remainders from last to first in a structure is the octal number system of the given decimal number.
def dec_to_octal(num):
# store octal number
octal_num = [0] * 100
# counter for octal
i = 0
while (num != 0):
# storing remainder in octal array
octal_num[i] = num % 8
num = int(num / 8)
i += 1
# printing octal number array in reverse order
for j in range(i - 1, -1, -1):
print(octal_num[j], end="")
print("\n")
# Driver code
if __name__ == '__main__':
dec_to_octal(8)
dec_to_octal(34)
dec_to_octal(44)
dec_to_octal(12)
Output of the above code:
10
42
54
14
Decimal to octal Python
Here is another approach to converting a decimal to an octal number system.
# Convert decimal to octal
def dec_to_octal(deci_num):
# initializations
octal_num = 0
count = 1
while(deci_num != 0):
# remainder is calculated
remainder = deci_num % 8
# storing the octalvalue
octal_num += remainder * count
# storing the exponential value
count = count * 10
deci_num //= 8
print(octal_num)
# Driver code
if __name__ == '__main__':
dec_to_octal(8)
dec_to_octal(12)
dec_to_octal(41)
dec_to_octal(24)
Output of the above code:
10
14
51
30
Decimal to octal Python using recursion
A recursion function is a function that is called by itself. In the given program, we call the recursion function to convert decimal to octal. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
# Python program to
# convert decimal to octal using recursion
def dec_to_octal(num):
if(num > 0):
dec_to_octal((int)(num/8))
print(num%8, end='')
# take user input
num = int(input('Enter a decimal number: '))
# Calling function and display result
print('Octal value: ', end='')
dec_to_octal(num)
Output of the above code:
Enter a decimal number: 100
Octal value: 144
Related Articles
Python convert list to numpy array
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module