C Program to Check Whether a Number is Even or Odd
In this example, we will learn to check whether a number entered by the user is even or odd.
Remainder :
To understand this example, we should have the knowledge of the following C programming topics:
What are even and odd numbers?
An even number is an integer which is divisible by 2. For example : 0, 2, 4, 6, 8.....etc.
An odd number is an integer which is not divisible by 2. For example: 1, 3, 5, 7, 9....etc.
Program to Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num % 2 == 0) {
printf("%d is a even number", num);
}
else {
printf("%d is a odd number", num);
}
return 0;
}
Output
Enter a number : 12
12 is a even number
Working of the above program
- First we declare a integer variable num.
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num % 2 == 0) {
printf("%d is a even number", num);
}
else {
printf("%d is a odd number", num);
}