C Program to check the given number is even or odd
Objective
Write a C program to check whether a given number is even or odd.
Algorithm / Approach
- Read an integer from the user.
- Use the Modulo operator (
%) to divide the number by 2 and check the remainder. - Condition:
if(a % 2 == 0). - If true (the remainder is 0), print "EVEN NUMBER".
- Else (the remainder is 1), print "ODD NUMBER".
main.c
#include<stdio.h>
int main( ) {
int a;
printf("Enter Values for A : ");
scanf("%d",&a);
if(a%2 == 0) {
printf("%d is EVEN NUMBER\n",a) ;
}
else {
printf("%d is ODD NUMBER\n",a);
}
return 0;
}
Expected Output
Enter Values for A : 18 18 is EVEN NUMBER
Explanation of the Program
- This is the standard algorithm for determining even/odd parity in programming.
- The Modulo operator (
%) returns the remainder of a division operation. Since all even numbers are perfectly divisible by 2, their remainder is always 0. Odd numbers always leave a remainder of 1.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)