×

Challenges

  1. Not started

  2. Not started

  3. Not started

  4. Not started

  5. Not started

  6. Not started

  7. Not started

  8. Not started

  9. Not started

  10. Not started

  11. Not started

  12. Not started

  13. Not started

  14. Not started

  15. Not started

  16. Not started

  17. Not started

  18. Not started

  19. Not started

  20. Not started

  21. Not started

Challenge 7.3

Convert a positive decimal number to a binary number

Challenge Level: Ready to expand

Requirement:

Write a program that asks the user to enter any decimal number as the input and displays the binary cards representing that number using '1' for dots showing and '0' for not showing as the output (this converts the decimal number to binary). Do this by working out the largest bit value needed, and then working down through smaller bit values.

Hints
  • Make variables called:

    • original and set its value to the input number given by the end user.
    • decimal_value and set its value to original (this is so we can still use the original number later)
    • bit_value and set its value to 1. Find the smallest bit value which is larger than the decimal_number by doubling value of bit_value while it is smaller or equal to decimal_number.
    • binary_number is a string variable and stores the binary cards needed (‘1’ for dots showing and ‘0’ for not showing).
  • Set the variable bit_value to 1 and find the smallest bit_value which is larger than decimal_number by multiplying bit_value by 2 while it is smaller or equal to decimal_number. You can do this by using a while loop.

  • Now divide the bit_value by 2 and check if decimal_number is greater than or equal to bit_value. If it is, add ‘1’ to the string binary_number and subtract bit_value from decimal_number. If not, add ‘0’ to the string binary_number. Repeat while bit_value is greater than 1. Display the value of binary_number as the output.

Programming Reminders
# Print a string directly
print("Hello World!")

# Print a variable
print(my_var)

Variables

# Set a variable as a string
fruit_name = "Apple"

# Set a variable as an integer 
pieces_of_fruit = 7

# Set a variable from a calculation
cost_of_fruit = pieces_of_fruit * cost_per_item

# Add one to a value
pieces_of_fruit += 1

Conditionals

# Find out the discount on fruit
if pieces_of_fruit > 100:
   print("Bulk discount applies")
elif pieces_of_fruit > 5:
   print("Discount applies")
else:
   print("No discount")

For loops

# Print numbers 0-9 - remember Python starts counting from 0 
for num in range(10):
    print(num)

While loops

# Print numbers 0-9 using a while loop and a variable
num = 0
while num < 10:
    print(num)

    # Increment the variable by one.
    # It will prevent an infinite loop!
    num += 1 

Lists

# Create a list of fruit 
fruit = ["Apple", "Banana", "Orange", "Pear"]

Functions

# Create a function which prints a greeting
def greeting(name):
    print("Hello " + name)

# Call the function
greeting("Spiderman")

Enter your code in the editor below

Your results will be displayed here

Input Expected output Received output Status
31
The binary representation for the number 31 is 11111

                
Not yet run ?
32
The binary representation for the number 32 is 100000

                
Not yet run ?
1
The binary representation for the number 1 is 1

                
Not yet run ?