Pages

Simple C Program to check if number is Even or Odd


This is a simple C programs which checks if a given number is even or odd. An even number is one that is completely divisible by 2 and an odd number is one which is not.



C Program: Check if given number is Even or Odd


#include <stdio.h>
int main()
{
    int number;/*Variable Declaration*/
    printf ("Enter any number: ");
    scanf ("%d", &number);/*Number input by the user*/
    
    if (number % 2 == 0)/*Checks if number is divisible by 2*/
    printf ("Number is Even");
    else
    printf ("Number is Odd");
    return 0;
}

Output



This program uses a single variable 'number' which stores the integer that is input by the user. The main task is done by the if statement:

    if (number % 2 == 0)/*Checks if number is divisible by 2*/
    printf ("Number is Even");
    else
    printf ("Number is Odd");

The if statement uses modular division. The modular division works slight differently than ordinary division in this way that it gives the remainder. So:
16 % 3 = 1
Therefore, if a number is completely divisible by any number then modular division will give 0.
Go through the code and in case of any confusion feel free to ask me by using the contact form below.


Don't Forget to Share...