Python program to convert decimal to binary using recursion
In this python programming guide, we are going to learn to:
-
write a python program to convert decimal to binary using recursion
Program to convert decimal to binary in python
The python program to convert decimal to binary using recursion is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
def decimal_to_binary(n):
if n > 1:
decimal_to_binary(n//2)
print(n%2, end = "")
if __name__ == "__main__":
number = int(input("Enter a number: "))
decimal_to_binary(number)
The output of python program to convert decimal to binary using recursion is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter a number: 7
111
Few important tips about this program
1. Here, we ask the user to enter a number and we type convert it to integer data type.
2. Then we call the function "decimal_to_binary" to convert the number to binary.
3. The function calls itself until the number itself becomes less than 1. We have used recursion here.
4. Python provides a bin() function whicn can directly convert a decimal number to binary.
Write a python program to convert decimal to binary using recursion 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