c语言31
31.编写程序,输入一批正整数(以零或负数为结束标志),求其中的偶数和。要求定义和调用函数even(n)判断数的奇偶性,当n为偶数时返回1,否则返回0。#include <stdio.h>
int even(int n)
{
if(n%2==0)
return 1;
else
return 0;
}
int main()
{
int n,sum=0;
printf("Input integers: ");
do{
scanf("%d",&n);
if(even(n)==1)
continue;
else
sum=sum+n;
}
while(n>0);
printf("The sum of the odd numbers is %d",sum);
return 0;
}
可以执行但是输入数据没有反应。
因为你的程序一直在死循环,没有退出while #include <stdio.h>
int even(int n)
{
if(n%2==0)
return 1;
else
return 0;
}
int main()
{
int n,sum=0;
printf("Input integers: ");
do{
scanf("%d",&n);
if(even(n)==1)
continue;
else
sum=sum+n;
}
while(n>0);
printf("The sum of the odd numbers is %d",sum);
return 0;
}
输出结果不对。
页:
[1]