Computer >> Computer tutorials >  >> Programming >> Python

How to Convert Decimal to Binary Using Recursion in Python?


Binary equivalent of a decimal number is obtained by printing in reverse order the remainder of successive division by 2. The recursive solution to this conversion is as follows:

def tobin(x):
    strbin=''
    if x>1:
        tobin(x//2)
    print (x%2, end='')

num=int(input('enter a number'))
tobin(num)

To test the output, run above code
enter a number25
11001
enter a number16
10000