A number that is completely divisible by 2 and the remainder is zero, is an even number. A number that is not completely divisible by 2 and the remainder is not zero, is an odd number. In this article, we will learn to check whether a number is even or odd in C programming language.
Algorithm
The simplest approach to check whether the given number is even or odd is to check for the remainder when the number is divided by 2.
- Use Modulus Operator ( % ) to check if the remainder obtained after dividing the given number N by 2 is 0 or 1.
- If the remainder is 0, print “Even”
- If the remainder is 1, print “Odd”.
Program to Check Even or Odd
C
// C program for the above approach #include <stdio.h> // Function to check if a // number is even or odd void checkEvenOdd( int N)
{ // Find remainder
int r = N % 2;
// Condition for even
if (r == 0)
{
printf ( "Even" );
}
// Otherwise
else {
printf ( "Odd" );
}
} // Driver Code int main()
{ // Given number N
int N = 101;
// Function Call
checkEvenOdd(N);
return 0;
} |
Output
Odd
Complexity Analysis
Time Complexity: O(1)
Auxiliary Space: O(1)
Refer to the complete article Check whether a given number is even or odd for more methods.