Advertisement
Guest User

Denary to Binary Conversion

a guest
Apr 4th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. # Denary to Binary Conversion
  2.  
  3. # Variables: denary_value, binary_string
  4.  
  5. print('This program will convert a denary (base 10) non-negative integer to a binary (base 2) number.')
  6.  
  7. denary_value = int(input('Enter a non-negative integer (base 10):  '))
  8. print ("denary value is ", denary_value)
  9. original_denary_value = denary_value
  10.  
  11. binary_string =''  # initialize the binary_string to an empty string
  12.  
  13. while (denary_value > 0):
  14.     binary_string = str(denary_value%2) + binary_string   # this is a concatenation of strings
  15.     print(binary_string)                                  # intermeidate step to check calculations (can delete)
  16.     denary_value = denary_value//2
  17.     print(denary_value)                                   # intermeidate step to check calculations (can delete)
  18.  
  19. print('The binary representation of ', original_denary_value, ' is: ', binary_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement