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

Complex Number Validation

Combine positivity, parity, digit count, and digit-sum rules. Students use named booleans and see why every condition must be checked before acceptance.

AdvancedPhase 6 - Category 1: Number LogicExample 1 of 10
complex-number-validation.py
1# Take a number
2num = int(input("Enter a number: "))
3
4# Check multiple conditions
5is_positive = num > 0
6is_even = num % 2 == 0
7is_two_digit = 10 <= num <= 99
8sum_of_digits = sum(int(d) for d in str(abs(num)))
9sum_is_even = sum_of_digits % 2 == 0
10
11if is_positive and is_even and is_two_digit and sum_is_even:
12 print("Number meets all criteria")
13else:
14 print("Number does not meet all criteria")

Output

Enter a number: 24
Number meets all criteria

Enter a number: 25
Number does not meet all criteria

In this one

  • Name independent predicates
  • Reject a negative input safely
  • Require every condition

How it works

  1. 1

    Read num and calculate positivity, parity, and digit-size booleans.

  2. 2

    Sum digits of abs(num) and test that sum's parity.

  3. 3

    Combine all four booleans with and.

Edge cases

What happens for 24?

What happens for 24?

All four criteria pass.

Why scan str(abs(num))?

Why scan str(abs(num))?

It prevents trying to convert a negative sign into a digit.

What's going on

Despite its title, this is an integer-rule validator, not Python complex-number arithmetic. It requires a positive even two-digit value whose digit sum is even.

Named booleans make each condition inspectable before the final and chain. Input 24 passes with digit sum six; 25 fails parity. The digit scan must use the magnitude, since attempting int('-') for negative input would fail before rejection.

What happens for 24? All four criteria pass.

Why scan str(abs(num))? It prevents trying to convert a negative sign into a digit.

Quiz

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

Question 1

Does one passing rule suffice?

Pick an answer to check it.

Question 2

What happens for 24?

Pick an answer to check it.

Questions people ask

What happens for 24?

All four criteria pass.

Why scan str(abs(num))?

It prevents trying to convert a negative sign into a digit.

Does one passing rule suffice?

No, every named condition must pass.