Skip to main content

๐Ÿ’š โ€” If Gate I: Even / Odd

Taskโ€‹

  • Create a file named even_odd.py.
  • Read an integer n.
  • If n is even, print: even. Otherwise print: odd.
  • Test with: 0, 1, 2, -3.
  • (Optional) Also print whether n is positive/negative/zero.
  • (Optional) Put the even/odd logic in a function.

Example runโ€‹

$ python even_odd.py
n: -3
odd
negative

Solution (ATTEMPT FIRST)โ€‹

Show spoiler code (even_odd.py)

Use n % 2 to check even/odd. Then a second if for sign.

even_odd.py
"""even_odd.py

If-gate challenge: decide which message to print.
"""

def even_or_odd(n: int) -> str:
"""Return 'even' or 'odd' based on parity."""
# % is 'mod' (remainder). Even numbers have remainder 0 when divided by 2.
return "even" if (n % 2 == 0) else "odd"

s = input("n: ").strip()
n = int(s) # assume valid int for now (you can add try/except as an upgrade)

print(even_or_odd(n))

# Optional: sign check
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")

# Alt parity approach (commented):
# if n % 2 == 0: print("even")
# else: print("odd")

Docs / Tutorialsโ€‹