C program to check if the given number is even or odd

C program to check if the given number is even or odd

This C program is to check if the given number is odd or even.For eg 12 is an even number as it is divisible by 2.

Logic

We use a simple logic which is if a number is divisible by 2 then it is an even number else an odd number.

Dry Run of the Program

Take input as an integer variable ‘n’  Let us take ‘n’ as 15

IF condition – if(n(15)%2=0) will result in false as 15 is not divisible by 2.So it will enter the else condition

%(MOD) means that it calculates the remainder as remainder in 15%2=1  , so 1 != 0.

Hence the output would be 15 is an odd number.

Program

#include<stdio.h>

void main() {
    int n;
    printf("Enter a number\n");
    scanf("%d",&n);
    
    if(n%2==0)
    {
        printf("\n%d is an even number",n);
    }
    else
    {
        printf("\n%d is an odd number",n);
    }
}

Output

Share Me!