Multiple Divisions - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for two numbers and displays their division and remainder. If 0 is entered as the second number, the program will warn the user with the message "Cannot divide by 0". If 0 is entered as the first number, the program will display "Bye!" and end. This task requires using conditional statements to handle edge cases like division by zero and stopping the program when the first number is 0. By completing this exercise, you will improve your skills in handling user input and performing basic mathematical operations such as division and finding the remainder in Python. You will also gain experience in using if statements to manage exceptions like division by zero and program termination based on specific conditions.

 Category

Mastering Flow Control

 Exercise

Multiple Divisions

 Objective

Develop a Python program that prompts the user for two numbers and displays their division and remainder. If 0 is entered as the second number, it will warn the user and end the program if 0 is entered as the first number. Examples:

First number? 10
Second number? 2
Division is 5
Remainder is 0

First number? 10
Second number? 0
Cannot divide by 0

First number? 10
Second number? 3
Division is 3
Remainder is 1

First number? 0
Bye!

 Example Python Exercise

 Copy Python Code
while True:
    # Prompt the user to enter the first number
    num1 = int(input("First number? "))
    
    # Check if the first number is zero
    if num1 == 0:
        print("Bye!")
        break
    
    # Prompt the user to enter the second number
    num2 = int(input("Second number? "))
    
    # Check if the second number is zero
    if num2 == 0:
        print("Cannot divide by 0")
    else:
        # Calculate and display the division and remainder
        division = num1 // num2
        remainder = num1 % num2
        print(f"Division is {division}")
        print(f"Remainder is {remainder}")

 Output

First number? 10
Second number? 2
Division is 5
Remainder is 0

First number? 10
Second number? 0
Cannot divide by 0

First number? 10
Second number? 3
Division is 3
Remainder is 1

First number? 0
Bye!

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

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.

  •  Multiple Times Tables (Using While)

    In this exercise, you will develop a Python program that displays multiplication tables from 2 to 6 using nested "while" loops. The program will iterate throug...

  •  Quadrilateral

    In this exercise, you will develop a Python program that prompts the user to enter a number and a width, and then displays a square of that width using ...

  •  Pause & Proceed

    In this exercise, you will develop a Python program that writes the even numbers from 10 to 20, both inclusive, except 16. The program will implement three different methods...

  •  Quadrilateral V2

    In this Python exercise, you will develop a program that prompts the user to input a number, width, and height and displays a rectangle of the specified...

  •  Iterative Patterns

    This Python program allows the user to input two numbers and displays all the numbers between them (inclusive) three times using different types of loops: "for"...

  •  Numerical Digits

    This Python program demonstrates how to calculate the number of digits in a positive integer by repeatedly dividing the number by 10. If the user enters a n...