#include <stdio.h>
int main() {
int n, num;
int even_count = 0, odd_count = 0;
// Input the number of integers to be checked
printf("Enter the number of integers: ");
scanf("%d", &n);
// Loop through the specified number of integers
for (int i = 0; i < n; i++) {
printf("Enter integer %d: ", i + 1);
scanf("%d", &num);
// Check if the number is even or odd
if (num % 2 == 0) {
even_count++; // Increment even count if the number is even
} else {
odd_count++; // Increment odd count if the number is odd
}
}
// Output the results
printf("Total even numbers: %d\n", even_count);
printf("Total odd numbers: %d\n", odd_count);
return 0; // Indicate that the program finished successfully
}
Explanation:
User Input: The program first prompts the user to enter the number of integers they want to check.Loop for Input: It uses a loop to read each integer.Check Odd or Even: For each integer, it checks if the number is even or odd using the modulus operator (%
):
- If
num % 2 == 0
, it increments the even count. - Otherwise, it increments the odd count.
Output Results: After processing all numbers, it prints the total counts of even and odd numbers.
Output:
Enter the number of integers: 5
Enter integer 1: 10
Enter integer 2: 21
Enter integer 3: 34
Enter integer 4: 43
Enter integer 5: 60
Total even numbers: 3
Total odd numbers: 2