C++20 sections · 1024 units
Open in Course

Problem: Is Even or Odd

Checking number parity

Write bool isEven(int n) that returns true if even, false if odd. Use n % 2 == 0. In main, read a number, call isEven, print "Even" or "Odd". This returns a bool, not a string.

Use the return value in an if. The function answers yes/no, the caller decides what to do. Try negative numbers. -4 is even, -3 is odd. Modulo works on negatives the same way. Test edge cases, not just happy paths.