Check Even/Odd Pair
Classify two integers as both even, both odd, or a mixed pair. A Python condition exercise on remainder tests, and, and using else for the remaining cases.
1# Take two numbers2num1 = int(input("Enter first number: "))3num2 = int(input("Enter second number: "))45# Check combinations6if num1 % 2 == 0 and num2 % 2 == 0:7 print("Both are even")8elif num1 % 2 != 0 and num2 % 2 != 0:9 print("Both are odd")10else:11 print("One is even, one is odd")
Output
Enter first number: 4 Enter second number: 6 Both are even Enter first number: 3 Enter second number: 5 Both are odd Enter first number: 4 Enter second number: 5 One is even, one is odd
In this one
- Classify integer pairs
- Combine parity tests
- Cover mixed cases with else
How it works
- 1
Read num1 and num2.
- 2
Check both-even, then both-odd.
- 3
Print the remaining mixed-pair label if neither holds.
Edge cases
Are zero and two both even?
Are zero and two both even?
Yes. Both leave remainder zero when divided by two.
Do -3 and 5 count as both odd?
Do -3 and 5 count as both odd?
Yes. Neither has remainder zero modulo two.
What's going on
Each number has its own parity, so the pair has three useful classifications. The first condition requires both remainders modulo two to be zero.
If both-even fails, the second condition checks that both remainders are nonzero. Everything left must be mixed. With 4 and 5, neither paired condition succeeds, so the final branch reports one even and one odd.
Are zero and two both even? Yes. Both leave remainder zero when divided by two.
Do -3 and 5 count as both odd? Yes. Neither has remainder zero modulo two.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why is no fourth classification needed?
Pick an answer to check it.
Question 2
Are zero and two both even?
Pick an answer to check it.
Questions people ask
Are zero and two both even?
Yes. Both leave remainder zero when divided by two.
Do -3 and 5 count as both odd?
Yes. Neither has remainder zero modulo two.
Why is no fourth classification needed?
The two mixed arrangements share one message.
๐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