Decimal to Octal in Python | In Computer Science, generally Octal number system used to store big data values. The octal system is the base 8 number system. We can also convert from Binary to Decimal, Decimal to Binary and Octal to Decimal, Octal to Binary and Binary to Octal also can be done.
In this post, we will write the programs for decimal to octal conversion in Python language. We will be given a decimal number and the python program to convert the given decimal number into an equivalent octal number.
Decimal | Octal |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 10 |
9 | 11 |
10 | 12 |
Note:- 8 & 9 are not present in the Octal number system.
Python Program to Convert Decimal to Octal
This python program using a while loop to convert decimal to octal. We can also take the help of a user-defined function. A function is a block of code that performs a specific task. We will take a decimal number while declaring the variables. Python program to convert decimal to octal using while loop and finally, the result will be displayed on the screen.
# Python program to convert decimal to octal
def DecimalOctal(num):
octal = [0] * 100
# counter for octal number array
i = 0
while (num != 0):
# store remainder in octal array
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='')
# take inputs
num = int(input('Enter a decimal number: '))
# calling function and display result
print('Octal value: ', end='')
DecimalOctal(num)
Output for the different input values:-
Enter a decimal number: 10
Octal value: 12
Enter a decimal number: 25
Octal value: 31
Enter a decimal number: 1258
Octal value: 2352
Program Without using Array
In the previous program, convert decimal to octal using array but in this program, convert decimal to octal without using an array.
# Python program to convert decimal to octal
def DecimalOctal(num):
octal, i = 0, 1
while (num != 0):
rem = num % 8 # remainder is calculated
octal += rem * i
# store exponential value
i = i*10
num //= 8
print(octal)
# take inputs
num = int(input('Enter a decimal number: '))
# calling function and display result
print('Octal value: ', end='')
DecimalOctal(num)
Output:-
Enter a decimal number: 5
Octal value: 5
Time Complexity of above program is 0(log N) and Auxiliary Space is 0(1).
Convert Decimal to Octal using Recursion
A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.
# Python program to convert decimal to octal using recursion
def DecimalOctal(num):
if(num > 0):
DecimalOctal((int)(num/8))
print(num%8, end='')
# take input
num = int(input('Enter a decimal number: '))
# calling function and display result
print('Octal value: ', end='')
DecimalOctal(num)
Output:-
Enter a decimal number: 100
Octal value: 144
Convert Decimal to Octal in Python
This is the simplest and easiest program in python because this program used a built-in function. We will take the decimal number when declaring the variable and print the octal value of the number using the oct() function.
oct() method returns the octal form of a passed number as a parameter. It returns an octal number in the form of 0oxx, where xx is the number form of octal representation.
# Python program to convert decimal to octal
# take input
num = int(input('Enter any decimal number: '))
# display result
print('Octal value:', oct(num))
Output:-
Enter any decimal number: 50
Octal value: 0o62
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!