Program to Check if a Number is Even or Odd

Problem Statement: Problem Statement: Write a C program to check whether a given number is even or odd.

Description:
This program checks whether an entered number is even or odd using the modulus operator (%). It introduces conditional statements (if-else) to control the program’s flow based on the result of the condition.

Usage:
After entering an integer, the program tells the user if the number is even or odd.

Code:

C
#include <stdio.h>

int main() {
    int num;

    // Asking user to input an integer
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Checking if the number is divisible by 2
    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }
    
    return 0;
}

Explanation:

  • if (num % 2 == 0) checks if the number is divisible by 2.
  • The modulo operator % is used to find the remainder of a division.
  • If the remainder is zero, the number is even; otherwise, it’s odd.

Test here:

Leave a Reply

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