Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def decim_to_binary(x):
- binary = ''
- y = 1
- power_of_two = 0
- number = x
- while(x / y > 1): # Find the max power of two
- y *= 2
- power_of_two += 1
- while(power_of_two >= 0):
- if(number == 0):
- binary += '0'
- elif(number < 2 ** power_of_two):
- binary += '0'
- elif(number >= 2 ** power_of_two):
- number -= 2 ** power_of_two
- binary += '1'
- power_of_two -= 1
- return binary
- question = input("Give decimal number: ")
- response = decim_to_binary(question)
- print("Decimal number " + str(question) + " is " + response + " in binary numbers.")
Add Comment
Please, Sign In to add comment