Python Decimal to Binary Conversion. Given a decimal number as input, the task is to write a Python program to convert the given decimal number into equivalent binary number. In this article, we will learn about the solution and approach to solve the given problem statement in Python.
How To Convert Decimal To Binary Conversion in Python?
Input : 22 Output :010110
Method #1: Recursive solution
DecimalToBinary(n): if n >= 1: DecimalToBinary(n // 2) print n % 2
Below is the implementation of above recursive solution:
def DecimalToBinary(n): if n >= 1: DecimalToBinary(n // 2) print(n % 2, end='') # Driver Code if __name__ == '__main__': # decimal value dec = 22 # Calling function DecimalToBinary(dec)
Attention Geeks! Strengthen your foundations with the Python Programming and learn the basics. If you have encountered any problem with the Code. Let us know in the Comment section.