Display the binary cards needed to represent a decimal number between 0 and 31

Challenge Level: Ready to expand

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter a decimal number between 0 and 31 as the input and displays the 5 binary cards representing that number (B for displaying black cards with no dots, and W for displaying white cards with dots) as the output.

Testing examples:

Your program should display the outputs shown in this table for the given inputs provided:

Input Output
11 The binary representation for the number 11 is BWBWW
31 The binary representation for the number 31 is WWWWW
0 The binary representation for the number 0 is BBBBB

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked

ask [Please enter a number between 0 and 31:] and wait

if <<(number) > (bit value)> or <(number) = (bit value)>> then
else
end

repeat until <(bit value) = [1]>
end

say (join (join (join [The binary representation for the number ] (answer)) [ is ]) (cards))
set [number v] to (answer)

set [bit value v] to [32]

set [cards v] to []

set [bit value v] to ((bit value) / (2))

set [cards v] to (join (cards) [W])

set [number v] to ((number) - (bit value))

set [cards v] to (join (cards) [B])
Hints
  • Make variables called:

    • “number” and set its value to the input number given by the end user.
    • “bit value” and set its value to ‘32’.
    • “cards” is a string variable and stores the binary cards needed (‘B’ for black cards and ‘W’ for white cards).
  • Now divide the “bit value” by 2 and check if “number” is greater than or equal to “bit value”. If it is, add ‘W’ to string variable “cards” and subtract “bit value” from the “number”. If not, add ‘B’ to string variable “cards”. Repeat until “bit value” is equal to 1. Display the value of “cards” as the output.

  • Test your program with some values on the boundaries (for example test it with numbers 0 and 31).

Show Scratch solution

Python