C Program to Check Odd or Even Numbers

In this C program we will check whether a number is odd or even number. Numbers divisible by 2 are even numbers whereas numbers which are not divisible by 2 are odd numbers. Any even number can be represented in form of (2*N) whereas any odd number can be represented as (2*N + 1).

Even Numbers examples: 2, 6 , 10, 12
Odd Number examples: 3, 5, 9 ,15

C program to find a number is odd or even using modulus operator

This program first takes a number as input from user and stores it in integer variable 'number'. We can use modulus operator for checking whether a number is odd or even, if after dividing a number by 2 we get 0 as remainder(number%2 == 0) then it is even number or if we get 1 as a remainder (number%2 == 1) then it is odd number.

#include <stdio.h>

int main(){
    int number;
    printf("Enter a number \n");
    scanf("%d", &number);
    if(number%2 == 1){
        /* Input number is odd */
        printf("%d is Odd Number\n", number);
    } else {
        /* Input number is even */
        printf("%d is Even Number\n", number);
    }
    return 0;
}
Output
Enter a number : 5
5 is Odd Number
Enter a number : 4
4 is Even Number

C Program to check odd or even number using function

This program calls a user defined function oddOrEvenUsingModulus, that takes an integer as input and returns whether input number is odd or even(Returns 1 for Odd number, 0 for Even number). It divides the input number be 2 and if remainder is 0 then it returns 0(even number) otherwise 1(odd number).

#include <stdio.h>

int oddOrEvenUsingModulus(int number);

int main(){
    int number;
    printf("Enter a number \n");
    scanf("%d", &number);
    if(oddOrEvenUsingModulus(number) == 1){
        /* Input number is odd */
        printf("%d is Odd Number\n", number);
    } else {
        /* Input number is even */
        printf("%d is Even Number\n", number);
    }
    return 0;
}

int oddOrEvenUsingModulus(int number){
    if(number%2 == 0){
        return 0;
    } else {
        return 1;
    }
}
Output
Enter a number : 7
7 is Odd Number 
Enter a number : 12
12 is Even Number

C program to check odd or even numbers using bitwise operator

If the least significant bit of number is 0, then number is even otherwise number is odd. We can check least significant bit of any number by doing bitwise and with 1.

if(number & 1 == 0){ 
    even number 
} else {
    odd number
}

#include <stdio.h>

int oddOrEvenUsingBitwise(int number);
int main(){
    int number;
    printf("Enter a number \n");
    scanf("%d", &number);
    
    if(oddOrEvenUsingBitwise(number) == 1){
        /* Input number is odd */
        printf("%d is Odd Number\n", number);
    } else {
        /* Input number is even */
        printf("%d is Even Number\n", number);
    }
    
    return 0;
}

int oddOrEvenUsingBitwise(int number){
    if(number & 1 == 0){
        return 0;
    } else {
        return 1;
    }
}
Output
Enter a number : 3
3 is Odd Number

C program to check odd or even numbers using arithmetic operators

In C, when we divide an integer with another integer we always get an integer(integer/integer = integer)even if numerator is not divisible by denominator.
For Example
6/3 = 2
7/3 = 2
If we divide a number by 2 and then multiply it with 2, If result is equal to original number then the number is even otherwise odd.

if((N/2)*2 == N) {
     N is even
} else {
     N is odd 
}


#include <stdio.h>
 
int oddOrEven(int number);
int main(){
    int number;
    printf("Enter a number : ");
    scanf("%d", &number);
    if(oddOrEven(number) == 1){
        /* Input number is odd */
        printf("%d is Odd Number\n", number);
    } else {
        /* Input number is even */
        printf("%d is Even Number\n", number);
    }
    return 0;
}
 
int oddOrEven(int number){
    if((number/2)*2 == number){
        /* number is even  */
        return 0;
    } else {
        /* number is odd  */
        return 1;
    }
}
Output
Enter a number : 9
9 is Odd Number
Enter a number : 14
14 is Even Number

Related Topics
C program to reverse a number
C program to swap two numbers
C program to add n numbers
C program to calculate power of a number
C program to check year is leap year or not
C program to convert string to integer
C program to add two complex numbers
List of all C programs