C Program to Check Whether a Given Integer is Odd or Even

An integer is even if it is divisible by 2 with no remainder, and odd if dividing by 2 leaves a remainder of 1. Zero is even. Negative integers follow the same rule: −4 is even, −7 is odd. This program uses the modulo operator % to determine parity in a single if-else check.

Number n % 2 Result
4 0 Even
7 1 Odd
0 0 Even
−6 0 Even
−9 −1 (or 1) Odd

Note on negative numbers: In C89/C90, the sign of the result of % with a negative dividend is implementation-defined. -9 % 2 may be -1 or 1 depending on the compiler. The check n % 2 == 0 is always reliable for “even” — zero has a definite sign.

C Program: Odd or Even

/* Check whether a given integer is odd or even
 * Compile: gcc -ansi -Wall -Wextra oddeven.c -o oddeven */
#include <stdio.h>

int main(void)
{
    int n;
    printf("Enter an integer: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    if (n % 2 == 0)
        printf("%d is Even.\n", n);
    else
        printf("%d is Odd.\n", n);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra oddeven.c -o oddeven
./oddeven

Sample Output

Enter an integer: 4
4 is Even.

Enter an integer: -7
-7 is Odd.

Enter an integer: 0
0 is Even.

Code Explanation

  • Modulo operator %n % 2 gives the remainder when n is divided by 2. If the remainder is 0, the number is even; otherwise it is odd. This works for all non-negative integers and for negative integers where the “even” check uses == 0 (always unambiguous).
  • scanf return value checkscanf returns the number of items successfully read. Checking != 1 catches non-numeric input (e.g., the user types “abc”) and exits cleanly instead of using an uninitialized variable.
  • Why not bitwise AND?if (n & 1) is a common alternative: the least significant bit of any odd number is 1. It is faster on some architectures and avoids the sign ambiguity of % for negative numbers. Both are correct; n % 2 == 0 is clearer to read.

Bitwise Alternative

/* Bitwise AND version — works correctly for negative numbers too */
if (n & 1)
    printf("%d is Odd.\n", n);
else
    printf("%d is Even.\n", n);

The least significant bit is 1 for all odd integers regardless of sign, so n & 1 is always reliable.

What This Program Teaches

  • Modulo operator % — the remainder after integer division. 7 % 3 == 1, 8 % 4 == 0. Parity (even/odd) is the simplest use case; modulo also drives circular indexing, hash functions, and cyclic counters.
  • Bitwise AND for parityn & 1 tests the last binary bit. Even numbers end in 0, odd numbers end in 1 in binary. Understanding bit-level representation is essential for embedded and systems programming.
  • Input validation with scanf — always check the return value of scanf. A missing check means the program silently uses whatever garbage value happens to be in the variable, producing unpredictable output.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>