Skip to content
Circuit

Even or Odd in C

Conditions

Use the remainder operator to classify a positive, negative, or zero integer.

Complete program

#include <stdio.h>

int main(void) {
    int n;
    printf("Integer: ");
    if (scanf("%d", &n) != 1) return 1;
    if (n % 2 == 0) puts("Even");
    else puts("Odd");
    return 0;
}

Try in Circuit →

Opens a new program without replacing your saved code. Press Run when you are ready.

How it works

Dividing an even integer by two leaves a remainder of zero. Testing n % 2 == 0 also works for negative even numbers and zero.

Do not test n % 2 == 1 for all odd numbers: negative odd operands can produce -1. The else branch correctly covers every nonzero remainder. Test -3, 0, and 8 as boundary cases.

Sample input

7

Enter these values when prompted. The transcript below includes typed responses.

Expected output

Integer: 7
Odd

Concepts to practice

  • if / else
  • Remainder
  • Negative numbers

Predict a different input or change one value, then compare the result with your prediction. Keep changes within the documented bounds and read warnings before trusting an answer.