2022-04-19 15:22:36 -03:00
|
|
|
#Decimal to binary number converter
|
|
|
|
|
|
|
|
|
|
decimal = int(input("Insert a decimal number: "))
|
2022-04-19 15:53:52 -03:00
|
|
|
a = int(input("How many bits do you desire? "))
|
2022-04-19 15:22:36 -03:00
|
|
|
|
2022-04-19 15:53:52 -03:00
|
|
|
bits = [] #total bits, i used cards previously cause my professor gave examples of binary numbers using cards.
|
2022-04-19 15:22:36 -03:00
|
|
|
t = []
|
|
|
|
|
for i in range(a-1, -1,-1):
|
2022-04-19 15:53:52 -03:00
|
|
|
bits.append(2**i)
|
2022-04-19 15:22:36 -03:00
|
|
|
t.append(0)
|
|
|
|
|
|
|
|
|
|
r = decimal
|
|
|
|
|
g = 0
|
|
|
|
|
|
|
|
|
|
while r !=0:
|
2022-04-19 15:53:52 -03:00
|
|
|
if r >= bits[g]:
|
|
|
|
|
r = r - bits[g]
|
|
|
|
|
t[g] = bits[g]
|
2022-04-19 15:22:36 -03:00
|
|
|
if t[g] != 0:
|
|
|
|
|
t[g] = 1
|
|
|
|
|
print(r)
|
|
|
|
|
g = g + 1
|
|
|
|
|
print("Yor decimal number converted to Binary is:", *t, sep ="")
|