Binary Code - Python Programming Exercise

This Python program prompts the user to enter a decimal number and displays its equivalent in binary form. Instead of using the str() function, the program converts the decimal number into binary using successive divisions, where the number is repeatedly divided by 2, and the remainders are collected to form the binary representation. The program continues to execute, converting decimal numbers to binary, until the user enters the word "end", signaling the end of the program. This method offers an excellent demonstration of how to manually perform number conversions in Python. The program is an effective way to understand how binary conversion works at a lower level. Rather than relying on built-in functions like bin(), it showcases the use of division and modulo operations to break down the decimal number into its binary components. This task is a great exercise for anyone learning about number systems and loops in Python, as it helps reinforce the fundamentals of both mathematics and programming logic.

 Category

Python Data Types

 Exercise

Binary Code

 Objective

Develop a Python program that prompts the user for a decimal number and displays its equivalent in binary form. It should repeat until the user enters the word "end." You must not use "str", but successive divisions.

 Example Python Exercise

 Copy Python Code
# Repeat until the user enters the word 'end'
while True:
    # Prompt the user for a decimal number or 'end' to stop
    number = input("Enter a decimal number (or 'end' to stop): ")

    # If the user enters 'end', break the loop
    if number.lower() == 'end':
        break

    # Convert the input to an integer
    number = int(number)

    # Initialize the binary representation as an empty string
    binary = ""

    # Use successive divisions to convert to binary
    while number > 0:
        binary = str(number % 2) + binary  # Add remainder (0 or 1) to binary string
        number = number // 2  # Divide by 2 and discard the remainder

    # If the binary string is empty (input was 0), set binary to '0'
    if binary == "":
        binary = "0"

    # Display the binary equivalent
    print(f"Binary: {binary}")

 Output

Enter a decimal number (or 'end' to stop): 10
Binary: 1010

Enter a decimal number (or 'end' to stop): 255
Binary: 11111111

Enter a decimal number (or 'end' to stop): 5
Binary: 101

Enter a decimal number (or 'end' to stop): end

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Conditional Logic and Booleans

    This Python program uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if both numbers entered by the user are...

  •  Exception Handling V2

    This Python program prompts the user for a real number and displays its square root. The program uses a "try...except" block to handle any potential err...

  •  Character

    This Python program prompts the user to input three letters and then displays them in reverse order. The program uses basic input and string manipulation techn...

  •  Triangular Shape

    This Python program prompts the user to input a symbol and a width, then displays a triangle of the specified width using that symbol for the inner part...

  •  String Password

    This Python program prompts the user for a username and a password (both should be strings) and repeats the process until the correct credentials are en...

  •  Arithmetic - Conditional

    This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or divisio...