2015-04-03 260 views
-3

我想更改此代碼以從輸入中獲得20個數字並計算奇數的數量和偶數的數量?請任何人都可以幫忙?奇數和偶數計數器

#include <conio.h> 
#include <stdio.h> 
int main() 
{ 
int n; 
int odd=0; 
int even=0; 
    printf("\nEnter any number \n"); 
    scanf("%d",&n);  
if(n%2!=0) 
{ 
    printf("%d is an odd number",n); 
    odd++; 
} 
else 
{ 
    printf("%d is an even number",n); 
    even++; 
} 
printf("\n odd%d/even%d",odd,even); 
} 
+2

你知道你已經把這裏的代碼? (不)它做什麼? – 2015-04-03 10:54:02

+0

它是轉讓嗎? – Mukit09 2015-04-03 10:54:28

+0

@MukitChowdhury seond嘗試[this](http://stackoverflow.com/q/29428260/2173917)之一。 :-) – 2015-04-03 10:55:31

回答

1

這裏是一個解決您的問題的函數:

提示:您需要一個循環來把你輸入20次。

void countForJHikaam(){ 
    int n,i; 
    int odd=0; 
    int even=0; 

    for(i=0;i<20;i++){ 

     scanf("%d\n",&n); 
     if(n%2==0){ 
      even++; 
     }else{odd++;} 

    } 
    printf("Odds: %d, Evens: %d",odd,even); 
} 

它不會真正幫助你學習。現在去了解一個函數是什麼。

1
#include <conio.h> 
#include <stdio.h> 
int main() 
{ 
    int n; 
    int odd=0; 
    int even=0; 
    printf("\nEnter any number \n"); 

    while(scanf("%d",&n))  
    (n%2) ? (++odd) : (++even); 

    printf("\n odd%d/even%d",odd,even); 
} 
1
#include<stdio.h> 
main() 
{ 
    int odd=0,even=0,no,count=20; 
    printf("Enter the 20 numbers...\n"); 
here: 
    scanf("%d",&no); 
    (no%2==0)? odd++ : even++ ;` 
    count--; 
    if(count>0) 
    goto here; 

    printf("No of odd numbers... :%d\n",odd); 
    printf("No of even numbers... :%d\n",even); 
}