Check Divisible by 5 and 11 in Python
Both is not the same as either: test Python remainders for 5 and 11 together. Beginner examples use 55, zero, and a number that satisfies only one condition.
1# Program to check if a number is divisible by both 5 and 1123num = int(input("Enter an integer: "))45if num % 5 == 0 and num % 11 == 0:6 print(num, "is divisible by both 5 and 11")7else:8 print(num, "is not divisible by both 5 and 11")
Output
55 is divisible by both 5 and 11
55 is 5 × 11, so both remainder checks return zero.
In this one
- Combine remainder tests with and
- Distinguish both from either
- Recognize multiples of an LCM
How it works
- 1
Read num as an integer.
- 2
Check for zero remainders under both 5 and 11.
- 3
Print success only if the combined condition is True.
Edge cases
One divisor only
Enter 10.
The first remainder is zero but the second is not, so the result is negative.
Zero
Enter 0.
Both remainders are zero; zero is divisible by each nonzero divisor.
What's going on
num % 5 == 0 and num % 11 == 0. Read the word both in the title. One successful remainder test is not enough.
55 leaves zero under both divisors, so it passes. 10 is divisible by 5 but leaves a remainder under 11 and fails. Python short-circuits and: if the first test is False, the second need not be evaluated.
Using or is the usual bug. It would accept 10 even though 11 does not divide it. Since 5 and 11 have no common factor above one, their least common multiple is 55; multiples of 55 pass this test. Zero and negative multiples pass too under the ordinary mathematical definition of divisibility.
After this program
If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why use and rather than or?
Pick an answer to check it.
Question 2
Does -55 pass?
Pick an answer to check it.
Questions people ask
Why use and rather than or?
Both divisibility requirements must hold, while or would accept either one.
Does -55 pass?
Yes. It leaves zero remainder with both divisors just like 55.
Could this be tested with one modulus?
Yes. For these divisors, num % 55 == 0 expresses the same requirement.