有两种方法:
一是判别偶数:
#include <stdio.h>
int main() {
int numbers[] = {10, 13, 6, 7, 4};;
int sum = 0;
int index;
for (index = 0; index < 5; index++) {
if (numbers[index]%2 == 0){
sum += numbers[index];
}
}
printf("The sum is %d\n", sum);
return 0;
}
二是判别奇数:
#include <stdio.h>
int main() {
int numbers[] = {10, 13, 6, 7, 4};
int sum = 0;
int index;
for (index = 0; index < 5; index++) {
if (numbers[index]%2 != 0){
continue;
}
sum += numbers[index];
}
printf("The sum is %d\n", sum);
return 0;
}