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.
1# Take a number as input2num = int(input("Enter a number: "))34# Check if divisible by both 3 and 55if 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
Convert input to an integer.
- 2
Check remainders for three and five.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum