Guest User

Untitled

a guest
Feb 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. def decim_to_binary(x):
  2. binary = ''
  3. y = 1
  4. power_of_two = 0
  5. number = x
  6.  
  7. while(x / y > 1): # Find the max power of two
  8. y *= 2
  9. power_of_two += 1
  10.  
  11. while(power_of_two >= 0):
  12. if(number == 0):
  13. binary += '0'
  14. elif(number < 2 ** power_of_two):
  15. binary += '0'
  16. elif(number >= 2 ** power_of_two):
  17. number -= 2 ** power_of_two
  18. binary += '1'
  19. power_of_two -= 1
  20. return binary
  21.  
  22.  
  23. question = input("Give decimal number: ")
  24. response = decim_to_binary(question)
  25.  
  26. print("Decimal number " + str(question) + " is " + response + " in binary numbers.")
Add Comment
Please, Sign In to add comment