C程序从一组5个整数计算整数

时间:2017-02-11 06:17:37

标签: c if-statement count sum int

我正在尝试编写一个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);
}

关于该怎么做的任何想法?

4 个答案:

答案 0 :(得分:1)

您的目标没有根据需要明确说明:

  • 你想要对所有偶数整数求一个忽略的奇数整数吗?

  • 您是否希望所有整数均匀并拒绝输入(如果它不包含?)

无论哪种方式,您的程序都会因多种原因而失败:

  • if ((n1 | n2 | n3 | n4 | n5) % 2) == 0) 没有任何用处:它只检查最后一个整数是否为偶数。你可以检查所有整数是否都是这个

    if
  • 您没有使用大括号对{正文中的说明进行分组。与Python不同,缩进在C中不起作用,您必须在多个指令周围使用大括号(}if)在elsewhile,{{1}之后形成一个块},for等。

以下是代码的修改版本,它忽略了奇数:

#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;
}

答案 1 :(得分:0)

偶数的简单测试除以2并测试0余数

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

只需使用for循环并逐步添加所有证明均匀

的内容

这适用于float或int就好了

答案 2 :(得分: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];
    }
}

答案 3 :(得分:0)

<强> QUES 即可。你想做什么?

Ans。你想编写一个C程序,从一组5个整数计算整数。这使得输出为不。在5个整数的集合中偶数整数

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

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

要求是:

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

    代码中还有一个问题: -

    #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.
    

    <强>解决方案: -

    $add_date = date("Y-m-d H:m:s"); 
    $expiry_date = new DateTime($add_date);
    $expiry_date ->modify("+60 days");
    echo $expiry_date ->format("Y-m-d H:m:s");
    

    你必须使用数组和使用循环编写相同的代码。它会变得很小而且执行起来很快:)