How to Write a C Program to Check Odd or Even numbers using If Statement and Conditional Operator with an Example of each? If a number is divisible by 2, it is an even number. For example, 2, 4, 10, -20, etc are even numbers. If an integer is not exactly divisible by 2 (a non-zero remainder), it is an odd number. For example, 3, 5, 7, -15, -19, etc.
C Program to Check Odd or Even using IF Condition
This program allows the user to enter an integer. Next, this program shows how to find or check even and odd numbers using the If statement.
In C Programming, we have an Arithmetic Operator called % (Module) to check the remainder of the division. Let’s use this operator to find the remainder. If the remainder is 0, then the number is even else odd.
Within this example, the printf statement will ask the user to Enter a value to check whether it is Even or Odd. The scanf statement will assign the user entered value to a number variable.
In the Next line, We declared the C If statement. Please refer to the Arithmetic Operator in C Programming article. If condition checks whether the remainder of the number divided by 2 is exactly equal to 0 or not.
- If the condition is True, it is an Even number.
- If the condition is False, it is Odd.
// using If Else
#include<stdio.h>
int main()
{
int number;
printf(" Enter an int value to check \n");
scanf("%d", &number);
if ( number%2 == 0 ) //Check whether remainder is 0 or not
printf("Given number %d is EVEN NUMBER \n", number);
else
printf("Given number %d is ODD NUMBER \n", number);
return 0;
}

Enter the second value to check for an even number.
Enter an int value to check Even or Odd
98
Given number 98 is EVEN NUMBER
In the above code, we used the if condition to check whether the remainder of the given integer is 0 or not and print as per the result.
C Program to Check Even or Odd using Conditional Operator
We can also use the Conditional operator to check whether the number 24 is odd or even in this programming language. Let’s see the example C program of this.
Refer to the Conditional operator in C article to understand the following example.
// using Conditional Operator
#include<stdio.h>
int main()
{
int number;
printf(" Enter an int value to check \n ");
scanf("%d",&number);
number%2 == 0 ? printf(" Even \n ") : printf(" Odd \n");
return 0;
}
OUTPUT
Enter an int value to check
24
Even
Try other values too
Enter an int value to check
19
Odd
C Program to check even or odd using functions
In the following example, we declared a user-defined reusable function to find whether the given number is even or odd. The evenOdd() function accepts an integer variable. Inside it, we use an if-else statement with a condition to check whether the given number is completely divisible by 2. If true, it prints that the number is even.
#include<stdio.h>
void evenOdd(int n) {
if (n % 2 == 0) {
printf("%d is an Even Number", n);
}
else {
printf("%d is an Odd Number", n);
}
}
int main(void)
{
int number;
printf("Enter any Number = ");
scanf("%d", &number);
evenOdd(number);
}
Enter any Number = -20
-20 is an Even Number
Enter any Number = 21
21 is an Odd Number
C Program to Check odd or even number using bitwise AND operator
In the following example, instead of using the modulo operator, we use the Bitwise AND operator. If the given number & (Bitwise AND) 1 returns 0, it is an even number. Otherwise, it is an odd number. Refer to the Bitwise Operators in C article.
10 = 1010
10 & 1 = 1010 & 0001 = 0000 = Even Number.
Let’s say Number = 9.
1001 (9) & 0001 = 0001 = Odd Number
#include<stdio.h>
int main(void)
{
int number = 10;
if(number & 1) {
printf("It is an Odd Number");
}
else {
printf("It is an Even Number");
}
}
It is an Even Number
Even or odd program in C using a for loop
When working with an array in C, we might use either a C for loop or a while loop to iterate over the array of numbers. There are situations where we need to check whether an array element (number) is even or odd. Based on it, we may perform certain operations.
Here, the for loop iterates over the array elements. On each iteration, the if statement checks whether the array element is divisible by 2. If true, print as an even number; otherwise, print odd number.
#include <stdio.h>
int main(void)
{
int n[] = {10, 9, 11, 20, 7};
for (int i = 0; i < 5; i++)
{
if (n[i] % 2 == 0)
{
printf("%d is an Even Number\n", n[i]);
}
else
{
printf("%d is an Odd Number\n", n[i]);
}
}
}
10 is an Even Number
9 is an Odd Number
11 is an Odd Number
20 is an Even Number
7 is an Odd Number
Also Read