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

Check if Divisible by Both 3 and 5

Check whether an integer is divisible by both three and five. This beginner Python lesson explains two remainder tests and why and differs from or.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 4 of 10
check-if-divisible-by-both-3-and-5.py
1# Take a number as input
2num = int(input("Enter a number: "))
3
4# Check if divisible by both 3 and 5
5if num % 3 == 0 and num % 5 == 0:
6 print("Divisible by both 3 and 5")
7else:
8 print("Not divisible by both 3 and 5")

Output

Enter a number: 15
Divisible by both 3 and 5

Enter a number: 9
Not divisible by both 3 and 5

In this one

  • Combine boolean conditions
  • Test two divisors
  • Distinguish and from or

How it works

  1. 1

    Convert input to an integer.

  2. 2

    Check remainders for three and five.

  3. 3

    Accept only if both tests succeed.

Edge cases

Does 9 qualify?

Does 9 qualify?

No. It is divisible by three but not by five.

Does zero qualify?

Does zero qualify?

Yes. Both remainders are zero.

What's going on

Two separate divisibility tests must succeed here. num % 3 == 0 asks about three; num % 5 == 0 asks about five. and joins the answers.

Input 15 passes both tests. Input 9 passes only the first, so it reaches else. Python can skip the second test when the first is false; either way, one failed test is enough to reject the number.

Does 9 qualify? No. It is divisible by three but not by five.

Does zero qualify? Yes. Both remainders are zero.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which connection requires both tests to pass?

Pick an answer to check it.

Question 2

Does 9 qualify?

Pick an answer to check it.

Questions people ask

Does 9 qualify?

No. It is divisible by three but not by five.

Does zero qualify?

Yes. Both remainders are zero.

Which connection requires both tests to pass?

and.