Given a decimal number as input, the task is to write a Python program to convert the given decimal number into equivalent binary number.
Input : 7 Output :111 Input :10 Output :1010
Method #1: Recursive solution
DecimalToBinary(num): if num >= 1: DecimalToBinary(num // 2) print num % 2
Below is the implementation of above recursive solution:
Method #2: Decimal to binary using in-built function
1000 10010 111
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.