Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive.

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive.

LANGUAGE: C++

CHALLENGE:

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 exactly one space.

Declare any variables that are needed.

SOLUTION:

int i=1;
int oddSum=0;
int evenSum=0;
int oddCount=0;
int evenCount=0;

while (i>0)
{
    cin >> i;
    if ((i % 2) == 0 && (i>0))
    {
        evenSum+=i;
        evenCount++;
    }

    if ((i % 2) != 0 && (i>0))
    {
        oddSum+=i;
        oddCount++;
    }
}

cout << evenSum << " " << oddSum << " " << evenCount << " " << oddCount << endl ;

Add comment