An integer that can be exactly divided by 2 is known as an even number. An integer that cannot be exactly divided by 2 is known as an odd number. In other words, a digit that contains 0, 2, 4, 6 or 8 as its last digit is known as even number.
Example 1: C Program to Check Even / Odd Number Using if…else Statement
#include<stdio.h> int main() { int number; printf(" Enter an integer" ); scanf("%d",&number ); if (number%2==0) { printf ("The entered number is EVEN."); } else { printf ("The entered number is ODD."); } return 0; }
Here, a variable number is declared as an integer data type and the user is asked to input an integer. The input that is given by the user is stored in the number variable, then number is divided (modulus division) by 2 and if the remainder is 0 then the number is exactly divided by 2 which means that the number is Even. But if remainder is not 0 then the number is not exactly divided by 2 which means that the number is Odd.
Example 2: C Program to Check Whether a Number is Even / Odd Using Ternary Operator
#include<stdio.h> int main() { int number; printf(“ Enter an integer n” ); scanf(“%d”,&number ); ( (number%2==0) ? printf (“n The entered number is EVEN.”) : printf (“n The entered number is ODD.”) ); return 0; }
Here, the statement
((number%2==0) ? printf (“n The entered number is EVEN.”) : printf (“n The entered number is ODD.”)
is short hand notation for:
if(number%2==0) printf (“n The entered number is EVEN.”); else printf (“n The entered number is ODD.”);
Output
Enter an integer 2 The entered number is EVEN.
Enter an integer 9 The entered number is ODD.