Skip to content
Home » Decimal to Binary in Python using recursion

Decimal to Binary in Python using recursion

Learn about Decimal to Binary in Python using recursion in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Decimal to Binary in Python using recursion

The recursive function DecimalBinary() in the below code takes a decimal value as an input and converts it into a binary representation.

Source code :

# Python program to convert decimal to binary using recursion

def DecimalBinary(n):  
    if n >= 1:
        DecimalBinary(n // 2)
    print(n % 2, end = '')
 
# input
num = int(input('Enter any decimal number: '))

# function call
DecimalBinary(num)

Output:

Enter any decimal number: 6
0110

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Python program to convert decimal to binary