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

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
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 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 operation under “Operators” which reports the remainder from division of first number by second number). Combine “remainder” values using the 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 block under the “Control”.
• Add “decimal number” (which is now ‘1’) to “binary number” and display it as the output.
Python

What it should look like

For the input 11 the code will output:

Please enter a decimal number:
The binary representation for the number 11 is 1011

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 original, which is number that user enters as the input, decimal_number, which is set to original (so we can use original later), 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 and round down the result to the nearest integer by using the "//" operator For example 11 // 2 will give you 5
• Store the remainder in variable remainder (use the modulo '%' operator to calculate the remainder. For example, 5 % 2 is 1.) Add remainder values using the addition operator to the front of the binary_number variable. Remember that when combining strings and integers, you need to cast the integers to strings. For example, 'hello' + 5 is not valid but 'hello' + str(5) will become 'hello5'.
• Repeat these blocks while decimal_number is greater than 1 (use a while loop)
• Add decimal_number (which is now 1) to binary_number and display it as the output.