C Program to Check a number is Even or Odd

CodingTute

Updated on:

C Program to Check a number is Even or Odd

In this example, you will learn one of the way to check a number is even or odd.

C Program to Check a number is Even or Odd

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is exactly divisible by 2
    if(number % 2 == 0)
        printf("%d is even number", number);
    else
        printf("%d is odd number", number);
    
    return 0;
}

Explanation

Mathematically, even numbers are perfectly divisible by 2 with remainder 0 and odd number leaves remainder 1 when divisible by 2. We use the same logic in the code.

if(number % 2 == 0)

modulus (%) operator is used to find the reminder, if the reminder is 0 then number is even else it will be odd.