Counting the number of Even and Odd numbers

So the assignment is to ask the user for certain numbers to display on the output. These certain numbers are then added up to show the total. Then the user must count how many even/odd numbers there are in the program.

i.e.
Total is 0
Please enter an integer: 20
Total is 20
You had 1 even numbers and 0 odd numbers.

I was able to get the first part of the problem figured out, but I cannot seem to get the part where I must count how many even/odd numbers there are in the program.

Any help from this community?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
  #include <string>
  using namespace std;
  int main (){
     int integer;
     int total = 20;
     int even_count = 0;
     int odd_count = 0;
     cout << "Total is 0" << endl;
     cout << "Please enter an integer: ";
     cin >> integer;
     cout << integer << endl;
     while ( total <= 30){
        cout << "Total is ";
        cout << total << endl;
        cout << "Please enter an integer: ";
        cin >> integer;
        cout << integer << endl;
        total = total + integer;
        if (integer/2 ==0){
           even_count++; 
        }
        else{
           odd_count++;
        }
     }
     cout << "You had " << even_count << " even numbers and ";
     cout << odd_count + 1 << " odd numbers." << endl;
     return 0;
  }

To check for even number, you could use the modulus operator %.
To give you an idea...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>


int main()
{
	int integer{ 0 };
	int even_count{ 0 };
	int odd_count{ 0 };
	int sentinel{ -1 }; // If user enter -1, the loop will stop.
	int total{ 0 };

	while (integer != sentinel)
	{		
		total += integer;
		std::cout << "Enter an integer: ";
		std::cin >> integer;

		if (integer == sentinel)
			continue;

		if (integer % 2 == 0) 
			even_count++;

		else
			odd_count++;
	}

	std::cout << "Running Total: " << total << std::endl;
	std::cout << "Even Number Count: " << even_count << std::endl;
	std::cout << "Odd Number Count: " << odd_count << std::endl;
	
	return 0;
}
Topic archived. No new replies allowed.