Binary representation of any decimal number by playing musical notes

Challenge Level: Ready to expand

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter any decimal number as the input and plays the musical notes representing that number in binary (high notes for dots showing and low notes for dots not showing).

Testing examples:

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

Input Output
255 high
high
high
high
high
high
high
high
100 high
high
low
low
high
low
low
1 high

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
whenclickedaskPlease enter a decimal number:andwait
playnote72for0.5beatsplaynote48for0.5beats
repeatuntildecimalnumber<bitvaluerepeatuntilbitvalue=1ifdecimalnumber>bitvalueordecimalnumber=bitvaluethenelse
setdecimal numbertoanswersetbit valueto1setbit valuetobitvalue*2setbit valuetobitvalue/2setdecimal numbertodecimalnumber-bitvalue
Hints
  • Make variables called:

    • “decimal number” and set its value to the input number given by the end user.
    • “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” until it is bigger than the “decimal number”.
  • 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 (Use the * block under “Operators” to multiply the “bit value” by 2) until it is larger than “decimal number”. You can do this by using a repeatuntil loop.

  • Now divide the “bit value” by 2 and check if “decimal number” is greater than or equal to “bit value”. If it is, play a high note and subtract “bit value” from the “decimal number”. If not, play a low note. Repeat until “bit value” is equal to 1.

  • To play a musical note use the block playnote60for0.5beats beats under the “Sound”. Type in the number “72” for a high note and “48” for a low note.

Show Scratch solution

Python

Extra Challenge

Modify your program so it would play a low note when the user enters the number zero as the input.