2025-03-31 19:37:23 +08:00

49 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-----------------------------------------------------------------------
【程序设计】
-----------------------------------------------------------------------
键盘输入五个正整数,编写函数求其中偶数之和,并输出结果
示例:
【请输入五个正整数】100 222 656 899 65
【这五个正整数中的偶数之和为】978
-------------------------------------------------------------------------
注意:请勿改动程序中的其他内容,请勿重新定义变量名。
------------------------------------------------------------------------*/
#include <stdio.h>
int sum_of_even(int arr[], int size);
int main()
{
int numbers[5];
int i;
int even_sum;
printf("【请输入五个正整数:】");
for (i = 0; i < 5; i++)
{
if (scanf("%d", &numbers[i]) != 1)
{
printf("输入有误,请确保输入了五个整数。\n");
return 1;
}
if (numbers[i] <= 0)
{
printf("输入有误!请确保输入的是正数。\n");
return 1;
}
}
even_sum = sum_of_even(numbers, 5);
printf("【这五个正整数中的偶数之和为:】%d\n", even_sum);
return 0;
}
int sum_of_even(int arr[], int size)
{
int sum = 0;
int i;
/**********Program**********/
for(i=0;i<size;i++){
if(arr[i]%2==0){
sum+= arr[i];
}
}
return sum;
/********** End **********/
}