Convert Decimal to Binary Using Recursion

Course- Python >

Source Code


# Python program to convert decimal number into binary number using recursive function

def binary(n):
   """Function to print binary number
   for the input decimal using recursion"""
   if n > 1:
       binary(n//2)
   print(n % 2,end = '')

# Take decimal number from user
dec = int(input("Enter an integer: "))
binary(dec)

Output


Enter an integer: 52
110100

In this program, we convert decimal number entered by the user into binary using a recursive function. Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.