2017-02-11 42 views
-1

我想写一个C程序,用户输入五个不同的整数并确定从这五个整数输入中的偶数的数量。这里是我目前的代码:从一组5个整数计算偶数整数的C程序

#include <stdio.h> 

int main() 
{ 
    int n1, n2, n3, n4, n5, sum; 

//user enters 5 integers 

    printf("Enter five different positive integers: \n"); 

//program scans for user input 

    scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5); 

//if statement to determine what integers are even 

    if(((n1,n2,n3,n4,n5)%2)==0) 

//sum of even integers 

     sum = n1 + n2 + n3 + n4 + n5; 

//program prints sum of even integers 

     printf("There are %d even integers in the input. \n", sum); 

//program prints if there are no even integers for the inputs 

    else 

     printf("There are no even integers in the input. \n"); 

    return(0); 
} 

任何想法怎么办?

+0

尝试使用数组和循环。在所有情况下。 – Peter

回答

0

偶数一个简单的测试是除以2和试验0剩余

if ( N % 2 == 0) { // This is even, inc your even counter here} 

只是使用for循环和步骤通相加所有证明即使

这适用于浮动或INT就好

0

(N1,N2,N3,N4,N5)是表达式序列,用逗号隔开,其评估的最后一个表达式,所以这样的:

//if statement to determine what integers are even 
if(((n1,n2,n3,n4,n5)%2)==0) 

仅确定的n5

整除可以使用的数组:

int n[5]; 

然后,在一个循环:

for (int i = 0; i < 5; i++) { 
    if ((n[i] % 2) == 0) { 
     sum += n[i]; 
    } 
} 
1

QUES。你想做什么?

Ans。你想写一个C程序从一组5个整数中统计整数。输出为没有。甚至整数在5个整数的集合。

我想你没有学过数组的概念。数组使这个问题很容易解决。但是,不要担心,我根据你的问题了解你。 但是, 你的任务是学习什么是数组为什么以及何时使用它?

----------------- xxx -------- xxx ---------- xxx ------- ---- XXX --------------------------------------------- --------------------------

要求是:

  1. 5变量(即NUM),其存储用户给出的值
  2. 一个变量,即计数的数量。甚至没有。它初始化为0,因为在给定输入之前,最初没有甚至没有。

----------------- xxx -------- xxx ---------- xxx ------ ----- XXX -------------------------------------------- --------------------------

1在你的代码更加的问题: -

if(((n1,n2,n3,n4,n5)%2)==0) //it only check for n5 because comma operator seperates the values and gives only last value(i.e. n5) for computaion. 

解决方案: -

#include <stdio.h> 

    int main() 
    { 
     int n1, n2, n3, n4, n5, count=0;  //var count works as a counter variable and it's value will update by 1 when any even no. encounters. 


    printf("Enter five different positive integers: \n"); 

    scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5); 


    if(n1%2==0) 
    { 
     count=count+1;  //now, count is set by 1 if the first input(n1) found even 
    } 


    if(n2%2==0) 
    { 
     count=count+1;  //now, count is set by 2 if the second input(n2) found even 
    } 


    if(n3%2==0) 
    { 
     count=count+1;  //now, count is set by 3 if the third input(n3) found even 
    } 

    if(n4%2==0) 
    { 
     count=count+1;  //now, count is set by 4 if the fourth input(n4) found even 
    } 


    if(n5%2==0) 
    { 
     count=count+1;  //now, count is set by 5 if the fifth input(n5) found even 
    } 




      printf("There are %d even integers in the input. \n", count); //count holds no. of even integers enconteres among 5 

//if count prints 0 it indicates no even no occured. 

you hav e使用数组和循环编写相同的代码。它成为你的代码小而快的执行:)

+0

如果您喜欢我的方式来解决您的问题,您可以投票给我的答案或接受答案。 ;) –

1

你的目标并未明确需要指出:

  • 你想总结所有偶数的忽略奇数类型?

  • 你是否想要所有的整数都是偶数,如果它不包含任何整数都拒绝输入?

无论哪种方式,你的程序出于多种原因失败:

  • if(((n1,n2,n3,n4,n5)%2)==0)没有任何用处:它不仅检查的最后一个整数是偶数。你可以检查所有整数是即使有这样的

    if ((n1 | n2 | n3 | n4 | n5) % 2) == 0) 
    
  • 你没有在if体用大括号组的说明。不像Python中,缩进发挥C中没有的角色,你必须使用括号({})周围的多个指令形成块之后ifelsewhilefor

下面是修改后的版本你忽略奇数代码:

#include <stdio.h> 

int main(void) { 
    int n1, n2, n3, n4, n5, sum, count; 

    // user enters 5 integers 
    printf("Enter five different positive integers:\n"); 

    // program scans for user input 

    if (scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5) != 5) { 
     printf("Invalid input\n"); 
     return 1; 
    } 

    // for each integer, add it if it is even 

    count = 0; 
    sum = 0; 

    if (n1 % 2 == 0) { 
     sum += n1; 
     count++; 
    } 
    if (n2 % 2 == 0) { 
     sum += n2; 
     count++; 
    } 
    if (n3 % 2 == 0) { 
     sum += n3; 
     count++; 
    } 
    if (n4 % 2 == 0) { 
     sum += n4; 
     count++; 
    } 
    if (n5 % 2 == 0) { 
     sum += n5; 
     count++; 
    } 

    if (count > 0) { 
     printf("There are %d even integers in the input, their sum is %d.\n", 
       count, sum); 
    } else { 
     //program prints if there are no even integers for the inputs 
     printf("There are no even integers in the input.\n"); 
    } 
    return 0; 
} 

用C的一些更高级的知识,你可以简化代码到这一点:

#include <stdio.h> 

int main(void) { 
    int n1, n2, n3, n4, n5, sum, count; 

    printf("Enter five different positive integers:\n"); 
    if (scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5) != 5) { 
     printf("Invalid input\n"); 
     return 1; 
    } 

    // use the low order bit to test oddness 
    count = 5 - ((n1 & 1) + (n2 & 1) + (n3 & 1) + (n4 & 1) + (n5 & 1)); 
    sum = n1 * !(n1 & 1) + n2 * !(n2 & 1) + n3 * !(n3 & 1) + 
      n4 * !(n4 & 1) + n4 * !(n4 & 1); 

    if (count > 0) { 
     printf("There are %d even integers in the input, their sum is %d.\n", 
       count, sum); 
    } else { 
     printf("There are no even integers in the input.\n"); 
    } 
    return 0; 
} 

但它实际上更复杂,可读性更差,并且不可证实更有效。

真正的改进是使用一个循环:

#include <stdio.h> 

int main(void) { 
    int i, n, sum = 0, count = 0; 

    printf("Enter five different positive integers:\n"); 
    for (i = 0; i < 5; i++) { 
     if (scanf("%d, &n) != 1) { 
      printf("Invalid input\n"); 
      return 1; 
     } 
     if (n % 2 == 0) { 
      sum += n; 
      count++; 
     } 
    } 

    if (count > 0) { 
     printf("There are %d even integers in the input, their sum is %d.\n", 
       count, sum); 
    } else { 
     printf("There are no even integers in the input.\n"); 
    } 
    return 0; 
}