C Program – Even or Odd Number
Last Updated :
17 Jul, 2023
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
#include <stdio.h>
void checkEvenOdd( int N)
{
int r = N % 2;
if (r == 0)
{
printf ( "Even" );
}
else
{
printf ( "Odd" );
}
}
int main()
{
int N = 101;
checkEvenOdd(N);
return 0;
}
|
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.
Share your thoughts in the comments
Please Login to comment...