Open In App

C Program to Check for Odd or Even Number

Last Updated : 18 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to check whether the given number is an odd number or an even number.

A number that is completely divisible by 2 is an even number and a number that is not completely divisible by 2 leaving a non-zero remainder is an odd number.

Example

Input: N = 4
Output: Even
Explanation: 4 is divisible by 2 with no remainder, so it is an even number.

Input: N = 7
Output: Odd
Explanation: 7 is not completely divisible by 2 leaving 1 as remainder, so it is an odd number.


How to Check Odd or Even Number in C?

In C, there are three different methods which we can use to check if the given number is an odd number or an even number:

1. Check Odd or Even Number Using Modulo Operator

The simplest way to check whether the given number is even or odd is to check for the remainder when the number is divided by 2. In C, the remainder of a number divided by another number is given by the modulus operator (%).

C Program to Check Whether the Given Number is Even or Odd

C
// C Program to Check Even or Odd Using Modulo Operator
#include <stdio.h>

void checkOddEven(int N) {
  
    // Find the remainder
    int r = N % 2;

    // Condition for even
    if (r == 0)  {
        printf("Even");
    }
  
    // Condition for odd number
    else  {
        printf("Odd");
    }
}

int main() {
    int N = 101;
    checkOddEven(N);
    return 0;
}

Output
Odd

Time Complexity: O(1)
Auxiliary Space: O(1)

2. Check Odd or Even Number Using Bitwise AND Operator

In odd numbers’ binary representation, the least significant bit (LSB) of the number is always set i.e. always 1 while for even numbers, it is always unset i.e. 0. We can use this property to extract the LSB using the bitwise AND operator (&) to check the least significant bit (LSB) of the number.

Below are the steps to implement the above approach:

  • Use the bitwise AND operator on the given number with mask = 1 to extract the LSB of the number.
  • If the result is 0, the given number is even.
  • If the result is 1, the given number is odd.

C Program to Check for Even or Odd Number Using Bitwise AND Operator

C
// C Program to Check Even or Odd Using Bitwise
// AND Operator
#include <stdio.h>

void checkEvenOdd(int N) {
  
    // Check if the number is even or odd using bitwise
  	// AND operator
    if (N & 1) {
        printf("Odd\n");
    }
    else {
        printf("Even\n");
    }
}

int main() {
    int N = 7;
  	checkEvenOdd(N);
    return 0;
}

Output
Odd

Time Complexity: O(1)
Auxiliary Space: O(1)

3. Check Odd or Even Number Using Shift Operator

The LSB in the odd number is always set i.e. 1. So, if we right shift the number by one and then left shift it by one again, then the LSB will become 0 making it unequal to the original number. While, if we do that to even number, it won’t change because the LSB is already 0 from the start.

C Program to Check for Odd or Even Number Using Shift Operator

C
// C Program to Check Even or Odd Using Bitwise AND Operator
#include <stdio.h>

void checkEvenOdd(int num) {
    int temp = num;
    temp = temp >> 1;
    temp = temp << 1;

    // Check if the number is even or odd using bitwise AND operator
    if (temp == num) {
        printf("Even\n");
    }
    else {
        printf("Odd\n");
    }
}

int main() {
    int num = 7;
    checkEvenOdd(num);
    return 0;
}

Output
Odd

Time Complexity: O(1)
Auxiliary Space: O(1)

Conclusion

Checking whether a number is even or odd can be efficiently implemented using various methods such as the modulo operator, bitwise AND operator, and shift operators. All methods have constant time and space complexity, making them suitable for quick and efficient checks.



Next Article

Similar Reads