實驗5-3 使用函式求奇數和 (15分)
阿新 • • 發佈:2019-02-04
http://pta.patest.cn/pta/test/13/exam/3/question/461
#include <stdio.h> #define MAXN 10 int even( int n ); int OddSum( int List[], int N ); int main() { int List[MAXN], N, i; scanf("%d", &N); printf("Sum of ( "); for ( i=0; i<N; i++ ) { scanf("%d", &List[i]); if ( even(List[i])==0 ) printf("%d ", List[i]); } printf(") = %d\n", OddSum(List, N));
return 0;
}
int even( int n )
{
if(n%2==0)
{
return 1;
}else{
return 0;
}
}
int OddSum( int List[], int N )
{
int i,sum=0;
for(i=0;i<N;i++)
{
if(List[i]%2!=0)
{
sum=sum+List[i];
}
}
return sum;
}