读书人

c语言编撰从键盘上任意输入10个数分

发布时间: 2013-10-27 15:21:49 作者: rapoo

c语言编写从键盘上任意输入10个数,分别统计出输入的偶数或奇数分别的平均值

#include <stdio.h>

int main(int argc, const char * argv[])
{

int i,a;
int c=0,x=0,n=0,m=0;


for(i=1;i<10;i++)
{
scanf("%d",&a);
if(a%2==0)
{
n=n+a;
c++;

}
else
{
m=m+a;
x++;
}

}
if(c>0&&x>0)
{
printf("奇数的平均数是%f\n",(float)m/(float)x);
printf("偶数的平均数是%f\n",(float)n/(float)c);
}
else if(x==0)
{
printf("无奇数,偶数的平均值为:%f\n",(float)n/(float)c);
}
else
{
printf("无偶数,奇数的平均值为:%f\n",(float)m/(float)x);
}
return 0;

读书人网 >C语言

热点推荐