Write a C program to check whether a number is even or odd
Description: Take a number as input. Print if it is even or odd.
Hint:
- A number is even when the remainder of the division by 2 is zero.
C program to check even or odd
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("%d is even\n", n);
} else {
printf("%d is odd\n", n);
}
return 0;
}
Output:
Output of the above program is
Enter a number: 27
27 is odd
How it works
27 % 2gives 1, so the number is odd.- For negative numbers it also works, because
-4 % 2is 0.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |