晨曦到日暮 发表于 2018-1-16 15:44:59

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;
}
可以执行但是输入数据没有反应。

jewen 发表于 2018-1-16 18:59:26

因为你的程序一直在死循环,没有退出while

晨曦到日暮 发表于 2018-1-16 20:12:54

#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]
查看完整版本: c语言31