如何实现输入0时结束输入?
题目要求输入一行整数直到输入0,然后计算奇数和偶数(0除外)的个数以及求出它们的平均值。#include<stdio.h>
int main(void)
{
int a;
int n_even = 0;
int n_odd = 0;
int sum_even = 0;
int sum_odd = 0;
float average_even;
float average_odd;
while(scanf("%d",&a)==1)
{
if((a%2)==0 && a!='0')
{
n_even++;
sum_even+=a;
}
else
{
n_odd++;
sum_odd+=a;
}
}
average_even=sum_even/n_even;
average_odd=sum_odd/n_odd;
printf("there are %d even number,%d odd number.\n",n_even,n_odd);
printf("the average of all even numbers is %.1f.\n",average_even);
printf("the average of all odd numbers is %.1f.\n",average_odd);
return 0;
}