How to Convert Decimal to Binary Using Recursion in Python?

PythonServer Side ProgrammingProgramming

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
raja
Published on 06-Mar-2018 05:14:19
Advertisements