Odd or Even Number in C
Odd or Even Number in C
Here's a simple C program to determine whether a given number is odd or even:
#include <stdio.h>
int main() {
int number;
// Ask the user to enter a number
printf("Enter an integer: ");
scanf("%d", &number);
// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is even.\\n", number);
} else {
printf("%d is odd.\\n", number);
}
return 0;
}
Explanation:
- The program prompts the user to enter an integer.
- It then checks if the number is divisible by 2 using the modulo operator %
. If the result is 0, the number is even; otherwise, it's odd.
- The program prints whether the number is odd or even based on this check.
Comments
Post a Comment