basics · beginner · ~15 min

Even or odd

Use the modulo operator + a conditional to classify an integer.

Challenge

Tell odd numbers from even numbers

Your first real "your turn" exercise after Hello, World. You'll use the modulo operator — the workhorse C uses to check divisibility, walk arrays in circles, hash things, and more.

Task

Write a program that reads one integer from standard input and prints exactly:

  • even if the number is divisible by 2
  • odd otherwise

Function signature

int main(void);

Input

A single integer on stdin (positive, negative, or zero).

Output

The literal word even or odd, followed by a newline.

Rules

  • Use scanf to read the integer. Check that it actually read one.
  • Negative numbers count too: -4 is even, -3 is odd.
  • 0 is even.

Examples

stdin stdout
4 even\n
7 odd\n
0 even\n
-3 odd\n

Edge cases

  • A user who types a non-integer (e.g. abc) should make your program exit non-zero. (Easy to do — scanf returns 0 if it didn't read an int.)
  • INT_MIN's sign doesn't matter for parity; INT_MIN % 2 == 0 on every modern platform.

Hints

  1. Conceptual: the modulo operator % returns the remainder of integer division. n % 2 is 0 for even, 1 (or -1 for negatives) for odd.
  2. Implementation: int n; if (scanf("%d", &n) != 1) return 1; printf("%s\n", n % 2 == 0 ? "even" : "odd"); return 0;
  3. Common bug: checking if (n % 2 == 1) to detect odd. That's false for -3 on some platforms because -3 % 2 == -1. Compare against zero (even) and treat everything else as odd.

Common mistakes

  • Forgetting & on scanf("%d", &n) (passes the value, not the address — undefined behaviour).
  • Printing "Even" / "Odd" (capitalised). The grader compares case-sensitively.

Why this matters

Modulo is one of the most-used operators in real C code. Get comfortable with it now.

Input format

One integer on stdin.

Output format

even or odd followed by a newline.

Constraints

Use scanf. Check its return value. Output is exact, lowercase.

Starter code

int is_even(int n) {
    /* TODO */
    return 0;
}

Common mistakes

Missing & on scanf. Capitalising Even/Odd. Checking n % 2 == 1 for odd.

Edge cases to handle

Zero is even. Negatives still classify correctly. Non-integer input → exit non-zero.

Complexity

O(1).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.