Programming from zero

Check if a port number is even or odd

easyTypes and operators

Problem statement

The modulo operator, %, gives you the remainder of a division instead of the result. 7 % 2 is 1, because 7 divided by 2 leaves 1 left over. Any even number leaves a remainder of 0 when divided by 2; any odd number leaves 1.

flowchart LR A["port % 2"] --> B{"remainder"} B -->|"0"| C["even"] B -->|"1"| D["odd"] classDef op fill:#DBEAFE,stroke:#2563EB,color:#1E3A8A classDef check fill:#FEF3C7,stroke:#D97706,color:#92400E classDef even fill:#D1FAE5,stroke:#059669,color:#065F46 classDef odd fill:#FEE2E2,stroke:#DC2626,color:#991B1B class A op class B check class C even class D odd

Common mistake: using / instead of %.

TEXT
Wrong: port / 2

/ gives you the division result, a number like 4040, not a remainder. % is the operator built specifically to answer "what's left over" questions like this one.

Your task: given port = 8080, print "even" if it divides evenly by 2, or "odd" if it doesn't.

Expected output:

TEXT
even

Approach

The modulo operator is spelled the same way in all three languages, %. Each one uses it inside an if to decide which word to print:

port = 8080
if port % 2 == 0:
    print("even")
else:
    print("odd")