Check if One Number is Multiple of Other

Check if one of two given numbers is a multiple of the other.

IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 4 of 10
check-if-one-number-is-multiple-of-other.py
1# Take two numbers as input
2num1 = int(input("Enter first number: "))
3num2 = int(input("Enter second number: "))
4
5# Check if one is multiple of other
6if 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

What's going on

Check both directions: num1 % num2 and num2 % num1.

Key Concepts:

If num1 % num2 == 0, num1 is multiple of num2
If num2 % num1 == 0, num2 is multiple of num1
Check both possibilities