Beginner · logic

Even or Odd

Even means divisible by 2, and "divisible by 2" in code is n % 2 == 0. That single expression is the whole test; the if chooses which word to print.

At a glanceRead <code>n</code>; print <code>even</code> if <code>n % 2 == 0</code>, otherwise <code>odd</code>.

Problem

Input: One integer n (−10000 ≤ n ≤ 10000).

Output: even or odd — lowercase, exactly these words.

Samples:

InputOutput
7odd
10even

Negative numbers are in the range on purpose: −4 is even too. If your test only checks positive input you will never notice a sign bug.

Your solution

main.cpp — your solution
Custom input for "Run my input"

Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.

Stuck? Hints

Hint 1 — Modulus gives the remainder

n % 2 is the remainder after dividing by 2: 0 for even numbers, 1 (or −1 for negatives in C++) for odd ones. Comparing with == 0 is therefore exactly the evenness test — you never need to look at the sign.

Hint 2 — One if, one else

Two outputs mean one if (...) { ... } else { ... }. Do not write two separate ifs — the second would run even when the first matched, and you would print both words.

Hint 3 — No semicolon after the condition

if (n % 2 == 0); is a complete if with an empty body — the print statement after it becomes unconditional. This is one of the top three beginner bugs in every language that has it.

Solution (try it first!)
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n % 2 == 0) {
        cout << "even" << endl;
    } else {
        cout << "odd" << endl;
    }
    return 0;
}

The same modulus test becomes a counter in Even and Odd Count — now over an array instead of one number. If branching still feels new, lesson 3 of the course walks through it with a grade calculator.

Deep dive

Why this works

Divisibility by 2 is exactly n % 2 == 0, and it works for negative numbers too: C++ gives a remainder whose sign follows the dividend, so −4 % 2 is 0 and −3 % 2 is −1 — neither is zero unless the number is genuinely even. One test covers the whole range.

The cost

O(1) — one modulus and one comparison.

Where it goes wrong

  • Writing if (n % 2 == 1) for odd. With negatives, −3 % 2 is −1, not 1 — so the odd branch never fires. Test == 0 instead, or use != 0.
  • A stray semicolon after the condition (if (n % 2 == 0);) makes the body empty and the print unconditional.
  • Two independent ifs instead of if/else: both words can print.

Next step

The same test, now counting across a whole array: Even and Odd Count.

Two things to remember

  • Even = n % 2 == 0; test for zero, never for one, when negatives are possible.
  • Two outputs need one if/else, not two ifs.
🐞 This version fails — open it and try

Tests % 2 == 1 — every negative number slips through as even

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n % 2 == 1) {
        cout << "odd" << endl;      // bug: never true for negatives
    } else {
        cout << "even" << endl;
    }
    return 0;
}