Hey guys, very new to programming and need some help with a homework problem.
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. Declare any variables that are needed.
Note a few things:
- This does not need to be a complete program, just what is asked above.
- This need to be a do-while loop.
- The spaces between the numbers is important, but I don't know how to get spaces. How do I set up the output to be spaced numbers like 1 2 3 4 instead of 1234?
Here is what I have so far:
Code:
int num, sum=0;
int sumeven=0;
int numeven=0;
int totalnum=0;
do
{
cin >> num;
if (num % 2 == 0 && num >= 0)
{
sumeven = sumeven + num;
numeven++;
}
sum = sum + num;
totalnum++;
}
while (num>0);
cout<< sum, sumeven, numeven, totalnum;
Right now, the problem is the program is simply adding up ALL the numbers, not the odd, evens, etc. Also, I know I need to add numodd and sumodd still, but I am still just lost.
Again, I am very new to this so go easy on me .
Thanks in advance!
PS - Accidentally posted this in the C forum so I am reposting it here.