Program to convert decimal to octal in Python
In this python programming guide, we are going to learn
-
python program to convert decimal to octal using while loop
Python program to convert decimal to octal using while loop
The program to convert decimal to octal in python is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
def decimal_to_octal(n):
result = list()
while n:
res = n % 8
n = n // 8
result.append(res)
return result[::-1]
if __name__ == "__main__":
number = int(input("Enter a number: "))
ans = decimal_to_octal(number)
for i in ans:
print(i, end="")
The output of python program to convert decimal to octal using while loop is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter a number: 1223
2307
Few important points about this program
1. First we ask the user to enter a number and we convert it to integer data type.
2. Then we call the decimal_to_octal function which divides the number by 8 and makes it the new number and store the remainders in a list. Finally, we return the list in reversed format.
3. In the main body, we have just printed the list elements.
Python code to convert decimal to octal snapshot:
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Mar 27,2021