Write a C program to find whether the given number is odd or even.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0){
printf("%d is even.", num);
}
else{
printf("%d is odd.", num);
}
return 0;
}
Enter an integer: 7
7 is odd.
In the program, the integer entered by the user is stored in the variable num.
Then, whether num is perfectly divisible by 2 or not is checked using the modulus % operator.
If the number is perfectly divisible by 2, test expression number%2 == 0 evaluates to 1 (true). This means the number is even.
However, if the test expression evaluates to 0 (false), the number is odd.
0 Comments