Counting Even and Odd Numbers

I have to write a program with a switch and a loop. I think I understand how to do this...but my professor wants the program to count up to 8 positive integers, then display the number of even integers and the number of odd integers to the screen. How do I accomplish this counting? I can't seem to find it in my book.

Thanks!
How do I accomplish this counting?

With a loop.
1
2
3
4
5
6
7
for(1 to 8 input integers)
{
if(even)
   evennum+=1
if(odd)
   oddnum+=1
}
1
2
3
4
5
6
7
8
9
#include <iostream>

int main(void) {
      int even = 0, odd = 0;
      for(int i = 1; i <= 8; ++i)
            ++(i % 2 == 0?even:odd);
      std::cout << "Even: " << even << "\nOdd: " << odd;
      return 0;
}

Simply use a for loop and either the tenary operator or an if-else chain.
Last edited on
Topic archived. No new replies allowed.