C program check even or odd using if else

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.

Example:
Input number: 10
Output: 10 is even number


Required knowledge

Basic C programming, Arithmetic operator, If else

Logic to check even or odd

We know, if a number is clearly divisible by 2 then the number is even otherwise odd. Hence logic of the program is clear and simple, we only need to check whether the number is divisible by 2 or not. We have also seen in one of previous post how to check divisibility of any number by performing modular division.
Here in this program we only need to perform modular division. A number is even if num % 2 == 0 otherwise it is odd.

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)
    {
        printf("Number is Even.\n");
    }
    else
    {
        printf("Number is Odd.\n");
    }

    return 0;
} 




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

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

3 comments: