Check if One Number is Multiple of Other
Compare two integers for a multiple relationship in either direction. This Python lesson explains remainder tests, branch order, and the risk of zero divisors.
1# Take two numbers as input2num1 = int(input("Enter first number: "))3num2 = int(input("Enter second number: "))45# Check if one is multiple of other6if num1 % num2 == 0:7 print(f"{num1} is a multiple of {num2}")8elif num2 % num1 == 0:9 print(f"{num2} is a multiple of {num1}")10else:11 print("Neither is a multiple of the other")
Output
Enter first number: 15 Enter second number: 5 15 is a multiple of 5 Enter first number: 7 Enter second number: 3 Neither is a multiple of the other
In this one
- Test multiples in either direction
- Understand modulo divisors
- Recognize zero-divisor failures
How it works
- 1
Read two integers.
- 2
Try num1 modulo num2, then the reverse.
- 3
Print the successful relationship or neither message.
Edge cases
What if the second input is zero?
What if the second input is zero?
The first modulo operation raises ZeroDivisionError.
What if both positive inputs are equal?
What if both positive inputs are equal?
The first branch reports the first number as a multiple of the second.
What's going on
One number is a multiple of another when division leaves no remainder. The program first tests num1 % num2 and only tries the reverse direction if that test fails.
For 15 and 5, the first test succeeds and prints the relationship immediately. For 7 and 3, neither direction works. The order is important because modulo with a zero divisor raises ZeroDivisionError before a later branch can help.
What if the second input is zero? The first modulo operation raises ZeroDivisionError.
What if both positive inputs are equal? The first branch reports the first number as a multiple of the second.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does a zero remainder show?
Pick an answer to check it.
Question 2
What if the second input is zero?
Pick an answer to check it.
Questions people ask
What if the second input is zero?
The first modulo operation raises ZeroDivisionError.
What if both positive inputs are equal?
The first branch reports the first number as a multiple of the second.
What does a zero remainder show?
An exact multiple relationship.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum