C Program to Check Whether a Number is Odd or Even

Here we’ll see different techniques to check whether a number is odd or even.

Check odd or even using modulus operator

Even numbers are always divisible by 2. If you do the modulo operation on the given number with 2 and the reminder is 0,then the number is even, otherwise, odd. This principle is applied in the following program.

#include <stdio.h>

void main()
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num%2 == 0)
    {
        printf("%d is an even number.\n", num);
    }
    else
    {
        printf("%d is an odd number.\n", num);
    }
}

C program to check odd or even using bitwise operator

The least significant bit of an even number is always 0. Similar thing applied for the odd numbers. Least significant bit of an odd number is always 1. If you do bitwise AND operation between the given number and 1 and the result becomes 0, then the least significant bit of the number is 0. That means the number is even. Similarly, if the result becomes 1, the number is odd. This principle is applied in the following program.

#include <stdio.h>

void main()
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num & 1 == 0)
    {
        printf("%d is an even number.\n", num);
    }
    else
    {
        printf("%d is an odd number.\n", num);
    }
}

Check odd or even with div and multiply operators

You can also check whether a number is odd or even by using div and multiply operators on integers in C programming language. If you divide an even number by 2 and then multiple the result with 2, you’ll get back the original number. For example, you’ll divide 102 by 2, then the result is (102/2 =) 51. If you multiply the result 51 with 2, you’ll get back the original number (51*2 =) 102. This is not true for odd numbers. When you’ll divide an odd number by 2, you’ll loose the reminder 1 in the result. If you again multiply the result with 2, you’ll not get back the original number. For example, if you divide 101 by 2, the result will be (101/2 =) 50. The if you multiply the result with 2, the number you’ll get is (50*2 =) 100 which is not equal to the original number. The following C program is written using this logic.

#include <stdio.h>
void main()
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if ((n/2)*2 == n)
    {
        printf("%d is an even number.\n", num);
    }
    else
    {
        printf("%d is an odd number.\n", num);
    }
}

Output:

Enter an integer: 234
234 is an event number.

Enter an integer: 2231
2231 is an odd number.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

One thought on “C Program to Check Whether a Number is Odd or Even”

Leave a Reply

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

1
0
0
4
0
0