c语言将从键盘上输入的n(不超过20)个正整数存入数组,分别统计数据中奇数的和与偶数的和

速度求高手
2024-11-27 02:43:36
推荐回答(5个)
回答1:

#include
#include
int main()
{
int *a,n,sum_even = 0,sum_odd = 0;
printf("请输入n的值:");
scanf("%d",&n);
a = (int *)malloc(sizeof(int) * n);
printf("请输入%d个整数\n",n);
for(int i = 0;i < n;i++)
{
scanf("%d",&a[i]);
if(a[i] % 2)
sum_odd++;
else
sum_even++;
}
printf("奇数的个数为:%d\n",sum_odd);
printf("偶数的个数为:%d\n",sum_even);
free(a);
return 0;
}

回答2:

#include
void main()
{
int ,x,a=0,b=0;
whil(printf("请输入正整数,输入负数结束输入:"),scanf("%d",&x),x>=0)
{
if(x%2)
a+=x;
else
b+=x;
}
printf("奇数和为:%d,偶数和为:%d",a,b);
return;
}

回答3:

#include
void initar(int ar[])
{ int item;
int i=0;
printf("press -1 end input,please input num>:");
while(item!=-1)
{
scanf("%d",&item);
ar[i++]=item;
}
}
void sum(int ar[], int &sumeven,int &oddsum)
{
for(int i=0;i {
if(ar[i]%2==0)
{
sumeven+=ar[i];
}
else
oddsum+=ar[i];
}
}
int main()
{
int ar[20];
int sumeven=0;
int oddsum=0;
initar(ar);
sum(ar,sumeven,oddsum);
printf("evensum is :%d\n",&sumeven);
printf("oddsum is :%d\n",&oddsum);
}

回答4:

main(){
int a[20];
int sum1 = 0;
int sum2 = 0;
for(int i=0;i<20;i++){
int n;
scanf(a[i],&n);
}
for(int j=0;jif(a[i]%2==0){
sum1 = sum1+a[i];
}else{
sum2 = sum2+a[i];
}
}
printf("sum1=%d",sum1);
printf("sum2=%d",sum2);

}

回答5:

int a[20];
int sumSingle = 0;
int sumEven = 0;
for(int i = 0; i < 20; i ++)
{
scanf("%d",&a[i]);
}
for(int i = 0; i < 20; i++)
{
if(a[i]%2 == 0)
{
sumEven + = a[i];
}
else sumSingle + = a[i];
}