C Program to Check Odd or Even Numbers

  • Write a C program to 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.

/*
* C Program to check a number is odd or even using modulus operator
*/
#include <stdio.h>
#include <conio.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);
    }
    getch();
    return 0;
}
Program 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).

/*
* C Program to check a number is odd or even using function
*/
#include <stdio.h>
#include <conio.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);
    }
    getch();
    return 0;
}

/*
 * Function to check passed number is odd or even 
 * using modulus operator
 * Returns 1 for Odd number, 0 for Even number
 */
int oddOrEvenUsingModulus(int number){
    if(number%2 == 0){
        /* number is divisible by 2 */
        return 0;
    } else {
        /* number is not divisible by 2 */
        return 1;
    }
}
Program Output 1
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
}

/*
* C Program to check a number is odd or even 
* using bitwise operator
*/
#include <stdio.h>
#include <conio.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);
    }
    getch();
    return 0;
}

/*
 * Function to check passed number is odd or even 
 * using bitwise operator
 * Returns 1 for Odd number, 0 for Even number
 */
int oddOrEvenUsingBitwise(int number){
    if(number & 1 == 0){
        /* Least significant bit of number is 0, hence number is even  */
        return 0;
    } else {
        /* Least significant bit of number is 1, hence number is odd  */
        return 1;
    }
}
Program 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 
}


/*
* C Program to check a number is odd or even
* using arithemetic operators
*/
#include <stdio.h>
#include <conio.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);
    }
    getch();
    return 0;
}
 
/*
 * Function to check passed number is odd or even
 * Returns 1 for Odd number, 0 for Even number
 */
int oddOrEven(int number){
    if((number/2)*2 == number){
        /* number is even  */
        return 0;
    } else {
        /* number is odd  */
        return 1;
    }
}
Program 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