Output the sums of the even and odd numbers for a list of input values

I need to write a program that prompts the user to input a number of integers, then displays how many even numbers (including zeros) and how many odd numbers there are. Then the program needs to display the sum of the even numbers and the sum of the odd numbers. I've got the first half done, but I can't figure out how to display the sums. Here is what I've got:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>

using namespace std;

const int N = 10;

int main()
{
	int counter;
	int number;
	int zeros = 0;
	int odds = 0;
	int evens = 0;
	int exit;

	cout << "Please enter " << N << " integers. They can be positive, negative, or zero." << endl;
	
	for (counter = 1; counter <= N; counter++)
	{
		cin >> number;
		
		switch (number % 2)
		{
		case 0:
			evens++;
			if (number == 0)
				zeros++;
			break;
		case 1:
		case -1:
			odds++;
		}
	}

	cout << endl;

	cout << "There are " << evens << " even numbers, which includes " << zeros << " zeros." << endl;
	cout << "The sum of the even numbers is " << SUMEVEN << endl;
	cout << " " << endl;
	cout << "There are " << odds << " odd numbers." << endl;
	cout << "The sum of the odd numbers is " << SUMODD << endl;
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
}


I put SUMEVEN where I am going to put the sum of the even numbers and SUMODD for the sum of the odd numbers. How do I do this?
Last edited on
You shall also calculate the sums in the same loop.
Declare and Initialize 2 integers like

1
2
int sumEven = 0 ;
int sumOdd = 0 ;

then make calculation in loop.
like
sumOdd += number
then display those values.

I shall also say that , using number&1 is more efficient than number%2.
Last edited on
I tried what you said but I must have done something wrong.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>

using namespace std;

const int N = 10;

int main()
{
	int counter;
	int number;
	int zeros = 0;
	int odds = 0;
	int evens = 0;
	int sumEven = 0;
	int sumOdd = 0;
	int exit;

	cout << "Please enter " << N << " integers. They can be positive, negative, or zero." << endl;
	
	for (counter = 1; counter <= N; counter++)
	{
		cin >> number;
		
		switch (number & 1)
		{
		case 0:
			evens++;
			if (number == 0)
				zeros++;
			sumEven = 0;
			sumEven = sumEven + number;
			break;
		case 1:
		case -1:
			odds++;
			sumOdd = 0;
			sumOdd = sumOdd + number;
		}
	}
	cout << endl;

	cout << "There are " << evens << " even numbers, which includes " << zeros << " zeros." << endl;
	cout << "The sum of the even numbers is " << sumEven << endl;
	cout << " " << endl;
	cout << "There are " << odds << " odd numbers." << endl;
	cout << "The sum of the odd numbers is " << sumOdd << endl;
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
}


My ten integers were 10 9 8 7 6 5 4 3 2 1 but it says the sum of the even numbers is 2 and the sum of the odd numbers is 1, which is incorrect.

Also, what is the difference in number % 2 and number & 1?
Last edited on
Because you are assigning 0 everytime.Do it just one time.
Like this ;
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>

using namespace std;

const int N = 10;

int main()
{
	int counter;
	int number;
	int zeros = 0;
	int odds = 0;
	int evens = 0;
	int sumEven = 0;
	int sumOdd = 0;
	int exit;

	cout << "Please enter " << N << " integers. They can be positive, negative, or zero." << endl;
	
	for (counter = 1; counter <= N; counter++)
	{
		cin >> number;
		
		switch (number & 1)
		{
		case 0:
			evens++;
			if (number == 0)
				zeros++;
			//sumEven = 0; <<- DELETE THIS 
			sumEven = sumEven + number;
			break;
		case 1:
		case -1:
			odds++;
		//	sumOdd = 0;        <--  DELETE THIS
			sumOdd = sumOdd + number;
		}
	}
	cout << endl;

	cout << "There are " << evens << " even numbers, which includes " << zeros << " zeros." << endl;
	cout << "The sum of the even numbers is " << sumEven << endl;
	cout << " " << endl;
	cout << "There are " << odds << " odd numbers." << endl;
	cout << "The sum of the odd numbers is " << sumOdd << endl;
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
}

number %2 and number&1 , gives the same result. You can choose one of them.As far as i remember number&1 works faster in general, because it's a bitwise operation.
Last edited on
Awesome. Thank you so much.
I'm trying to accomplish this same problem however I need to do it through a function. Can anyone help?
@kalel
Sure, post your code.
Topic archived. No new replies allowed.