2017-02-11 69 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; 
}