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
Show 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