In this program, you will learn how to convert a decimal number to a binary using a recursive function.
To understand this program to convert decimal to a binary number, you should have the knowledge to follow the Python programming topics:
- Python If…. else declaration
- Python functions
- Python recursion
The decimal number is converted to binary by successively dividing the number by 2 and printing the remainder in reverse order.
Program to convert decimal to a binary number using recursion
def convertToBinary(n): """Function to print binary number for the input decimal using recursion""" if n > 1: convertToBinary(n//2) print(n % 2,end = '') # decimal number dec = 34 convertToBinary(dec)
Output
100010
Ask your questions and clarify your/other doubts about how to convert a decimal number to binary using Python recursion by commenting. Python Documentation
Related programs: