×

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.4

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

Challenge Level: Ready to expand

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.

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.
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 ?