If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here is an example of a C program that checks if a number is even or odd:
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.", num);
} else {
printf("%d is odd.", num);
}
return 0;
}
The program prompts the user to enter an integer, which is stored in the variable "num". The modulus operator (%), which returns the remainder of a division operation, is used to check if the number is even or odd. If the remainder when "num" is divided by 2 is 0, the number is even. If the remainder is not 0, the number is odd. The program then prints out a message indicating whether the number is even or odd.
You can also use bitwise operator instead of modulus operator to check if a number is even or odd:
Copy code
if (num & 1 == 0) {
printf("%d is even.", num);
} else {
printf("%d is odd.", num);
}
Here the bitwise operator & will perform a bit-by-bit 'AND' operation. If the last bit of the number is 0 then the number is even otherwise it's odd.
Comments: 0