Sorry , Newbie here
i have no clue on how to do "Write a complete C program to read twenty (20) integer numbers. Then count and display the number of even and odd numbers among the twenty (20) integers read. Use a while loop to perform the repetition task."

is there any example ?
please help~:(

I only know do in For loop

#include <stdio.h>
#include <stdlib.h>

main()
{
      int n=0;
      int i=0;
      printf("This is even\n");
for(i=1;i<=20;i++)
      {
             if(i%2==0)
             printf("%d\n",i);
      }
      printf("This is odd\n");
      for(n=0;n<=20;n++)
      {
             if(n%2==1)
             printf("%d\n",n);
      }
system("PAUSE");
return 0;
}

any example for doing in while loop ?

Edited 3 Years Ago by Nick Evan: Fixed formatting

PLEASE! Use CODE tags when you send us code.

A while statement is quite simple :

while (condition)
{
/*do something*/
}

A condition is something you see in an if-statement also.
Example :

/*counting to three*/
int i = 0;
while (i<3)
{
    i = i + 1;
    printf(" The count is %d\n", i);
}

Code tags

Your problem states that you have to read 20 integers which means that you'll have to use scanf() to get the input from the user. Then you've to display the number of even and odd numbers present.

while(there are still nos to read) //or no of integers read is less than 20
{
  if(number divisible by two)
    increment even counter
  else
    increment odd counter
}

display odd and even counter

Use scanf and then check the input to see whether its odd or even and then just add the value to an evenTotal variable or oddTotal variable.

The while loop will be used to make sure the user enters exactly 20 values no more, no less.

#include <stdlib.h>
#include <stdio.h>

int main(){
int nrEvens = 0;
int nrOdds = 0;
int nrInputs = 0;
int currentValue;

while(nrInputs < 20){
printf("Enter a number: ");
scanf("%d", &currentValue);
if (currentValue % 2 == 0){
nrEvens++
} else{
nrOdds++;
}
nrInputs++;
}

printf("There were %d even values entered and %d odd values entered.", nrEvens, nrOdds);

return 0;
}

This is the correct code. I will add comments shortly to explain how this works. You should seriously consider learning how to do it on your own, otherwise, you will not pass your class.

>I will add comments shortly to explain how this works. You should seriously consider learning how to do it on your own, otherwise, you will not pass your class.
How come? When all [s]he needs to do is copy and paste.

>This is the correct code.
No it is not. Learn how to properly indent; and how to check the return of the scanf() function.

>I will add comments shortly to explain how this works. You should seriously consider learning how >to do it on your own, otherwise, you will not pass your class.
How come? When all [s]he needs to do is copy and paste.

>This is the correct code.
No it is not. Learn how to properly indent; and how to check the return of the scanf() function.

Lol, k

PS: @OP, like I said, I haven't coded in C in awhile, but I believe what the nerd above me was talking about is the following:

int result = scanf("%d", &currentValue);
      if (result == 0){
         return 0;
      }

Hardly exciting, I guess I learned quickly. Although no input checking was necessary since you stated in your problem that inputs were guaranteed to be integers.

No it wouldn't. And why are you arguing with me anyway? I already explained that I haven't programmed in C in quite a while. If you disagree with something I said, then it is on you to prove me wrong. There isn't much for me to prove since the code works. Btw, scanf returns the number of arguments successfully read. It can also return EOF but I hardly think that is a problem here, although it might be a good idea to add || EOF to that if statement.


To test to verify that the result would be 0 on bad input, go run the program and type in anything that isn't an integer.

This article has been dead for over six months. Start a new discussion instead.