How is the sum of all odd or even numbers found

Apr 2, 2016 at 9:08pm
How do you find the sum of all the numbers output from a loop?

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
  #include <iostream>

using namespace std;

int main()
{
	int a, b, c;

	cout << "Enter two integers ";
	cin >> a >> b;
	cout << endl;

	while (a > b)
	{
		cout << "The first number must "
			<< "be smaller than the second.";

		cout << "Enter two integers ";
		cin >> a >> b;
		cout << endl;
	}

	

	while ((a = a) && (a < b))
	{
		a = a + 2;
		cout << a << endl;
	}
	
		c = a + a;
		cout << c << endl;
	

	return 0;
}
Apr 3, 2016 at 12:06am
What is a?
What is b?
What is c?
Why do you need 2 integers?
Why does the second have to be larger?
What is the loop being output?
Apr 3, 2016 at 2:56am
What is your question kromari?

If your question is: "From a list of integers (numbers), how do you find the sum of all the even numbers and the sum of all the odd numbers respectively?"

Then, here is how:

1) Store the numbers in a vector<int>
2) Use the % (modulo) operator to figure out if the number is odd or even
3) Put the odd numbers in another vector<int>. Put the even numbers in another vector<int>
4) Create a int variable for each vector to store the sum of each vector's elements.

Literally all that's left is to store the sum of each vector in it's respective sum variable
Last edited on Apr 3, 2016 at 3:00am
Apr 3, 2016 at 4:16am
num % 2 = 0
else
num % 2 != 0

to find odd or even and then total them?
Last edited on Apr 3, 2016 at 4:27am
Topic archived. No new replies allowed.