C Program to Check Whether a Number is Even or Odd
This program takes a number as input and checks whether it is even or odd using the modulus operator (%).
Concept Overview
A number is even if it is divisible by 2, i.e., number % 2 == 0. Otherwise, it is odd. We use if-else statements to determine the result.
Program
C
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is an even number.\n", num);
else
printf("%d is an odd number.\n", num);
return 0;
}
Sample Output
Enter a number: 15 15 is an odd number.
Explanation
- `num % 2 == 0` – Checks if the number is divisible by 2 (even).
- `printf()` – Displays whether the number is even or odd.
Note: Note: You can also use the ternary operator `(num % 2 == 0) ? 'Even' : 'Odd'` for a compact version.
Codecrown