Python Program to Convert Decimal to Binary

Using Loop

To convert Decimal to binary we check if the number is divisible by 2, if yes then we take a string and append 0 to and and if not then append 1 to it and divide the number by 2 and do the same again.

finally we reverse the string and print the result

numb = 121
binary = ''

while(numb):
if(numb % 2 == 0):
binary += '0'
else:
binary += '1'
numb = numb//2

''.join(reversed(binary))

print(binary)

Using Inbuilt function

 converting decimal to binary is quite easy

a = bit(121)
print(a[2:])

Leave a Reply

Your email address will not be published.