C program check whether a number is even or odd

Previous Program Next Program

Write a C program to check whether a number is even or odd using if else. How to check whether a number is even or odd using if else in C program. Program to input any number from user and check whether the given number is even or odd. Logic to check even/odd in C program.

Example

Input

Input number: 10

Output

10 is even number

Required knowledge

Basic C programming, Arithmetic operator, If else

Logic to check even or odd

Mathematically we say, even number is a number which is completely divided by 2 leaving no remainder. Programatically if any number modulo divided by 2 and remainder is equal to 0. Then the number is even otherwise odd.

In the journey of C programming we are already familiar with modulo operator %. It returns the remainder of the division operation performed with two integers. For this exercise we only need to check whether the given number modulo divided by 2 is 0 or not. Hence the condition to do so is num % 2 == 0.

Note: Never confuse the modulo division % operator as the percentage operator. That returns the percentage.

Let us now implement the logic

Program to check even or odd

/**
 * C program to check even or odd number
 */

#include <stdio.h>

int main()
{
    int num;

    /* Reads number from user */
    printf("Enter any number to check even or odd: ");
    scanf("%d", &num);
    
    /* Check if the number is divisible by 2 then it is even */
    if(num%2 == 0)
    {
        // num % 2 is 0
        printf("Number is Even.\n");
    }
    else
    {
        //num % 2 is 1
        printf("Number is Odd.\n");
    }

    return 0;
} 

Advance your programming skills by learning this program using other methods:

Output
Enter any number to check even or odd: 11
Number is Odd

Happy coding ;)

You may also like

Previous Program Next Program

Labels: , ,