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

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.

IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 7 of 10
check-even-odd-pair.py
1# Take two numbers
2num1 = int(input("Enter first number: "))
3num2 = int(input("Enter second number: "))
4
5# Check combinations
6if 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. 1

    Read num1 and num2.

  2. 2

    Check both-even, then both-odd.

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