Convert a decimal number to a binary number (alternative method)

Challenge Level: Ready to expand

Learning outcomes

Students will be able to:

Requirement:

Using a different method from “Binary challenge 7.3”, 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). This method generates the number from right to left by observing that an even number has a 0 on the right, and an odd number as a 1 on the right.

Testing examples:

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

Input Output
31 The binary representation for the number 31 is 11111
32 The binary representation for the number 32 is 100000

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 decimal number:] and wait

repeat until <(decimal number) = [1]>
end

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

set [remainder v] to [0]

set [binary number v] to []

set [remainder v] to ((decimal number) mod (2))

set [decimal number v] to ([floor v] of ((decimal number) / (2)))

set [binary number v] to (join (remainder) (binary number))

set [binary number v] to (join (decimal number) (binary number))
Hints
  • This approach is based on observing that the right-hand bit value is easily identified (the remainder of the decimal number when divided by 2 will be 1 i.e. it's an odd number). The number can then be divided by 2, which moves all the digits one place to the right, and so the next bit becomes the right-hand bit.
  • Make variables called "decimal number", which is number that user enters as the input, “binary number”, which is type string and it will be used to store 0’s and 1’s that represent the binary number as the output, and “remainder”, which stores the remainders of values for “decimal number” divided by 2.
  • Divide “decimal number” by 2. Round down the result to the nearest integer by using the floor function (choose the “floor” option from the drop down menu ([sqrt v] of [9]) under the “Operators”) and set “decimal number” to this value. For example using “floor” function if you divide 11 by 2 would give you ‘5’.
  • Store the remainder in variable “remainder” (use the () mod ()operation under “Operators” which reports the remainder from division of first number by second number). Combine “remainder” values using the join [] [] block under the “Operators” and store the result in the “binary number” variable.
  • Repeat these blocks until “decimal number” is equal to ‘1’ (Use the repeat until <> block under the “Control”.
  • Add “decimal number” (which is now ‘1’) to “binary number” and display it as the output.

Show Scratch solution

Python