Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

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

In this one

  • Test multiples in either direction
  • Understand modulo divisors
  • Recognize zero-divisor failures

How it works

  1. 1

    Read two integers.

  2. 2

    Try num1 modulo num2, then the reverse.

  3. 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.