dimanche 24 octobre 2021

Using a loop to convert decimal to binary

I want to write a program that coverts the decimal numbers 0 to 9 into binary. I can code how to convert a decimal number to binary using repetitive division. However, I am trouble with creating a loop that will print out the decimal numbers 0 to 9 in their binary format.

Here is my code

number = 0

remainder = 0
x = ""
while number (0,9):
    remainder = (number%2)
    x = str(remainder) + x
    number = number//2
   
    (x[::-1])
    print(number, "is the binary of",x)

Aucun commentaire:

Enregistrer un commentaire