Check if One Number is Multiple of Other

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

Logic BuildingIntermediate
Logic Building
# Take two numbers as input
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Check if one is multiple of other
if num1 % num2 == 0:
    print(f"{num1} is a multiple of {num2}")
elif num2 % num1 == 0:
    print(f"{num2} is a multiple of {num1}")
else:
    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

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