Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
binary = ""
place_value = 1
denary = int(input("Give me a denary integer: "))
while denary >= place_value:
  binary = str(int(denary & place_value > 0)) + binary
  place_value = place_value * 2
print("The binary representation of",denary,"is",binary)

""" NB.  I've used a trick to avoid the need for an if...

Casting True or False to an integer gives 0 or 1, which I then cast to a character to add it to the start of the string called binary

If you prefer you could do it like this:

if denary & place_value > 0:
  binary = "1" + binary
else:
  binary = "0" + binary
"""