Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Codelabs C++ Exercise 11134 Write a loop that reads positive integers from stand

ID: 3839183 • Letter: C

Question

Codelabs C++ Exercise 11134

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 and the sum of all the odd integers read(The two sums  are separated by a space). Declare any variables that are needed.

My Submission:

{

int sume=0,sumo=0;

int n=9;

while(n > 0){
cin >> n;
if ((n % 2)==0 && (n > 0)){
sume+=n;
}

else{

sumo+=n;
}

}
cout <<"The sum of even is " <<sume<< ". ";

cout <<"The sum of odd is " <<sumo;


}

and this is what it tells me:

The Council agrees that:
          We think you might want to consider using: !=


Problems Detected:
          The value of _stdout is incorrect.

Any advice on how I can improve my code or how to resolve this problem?

Explanation / Answer


using ternary operator

using namespace std;

int main()
{
int m;

cout << "Enter an number: ";
cin >> m;
  
(m % 2 == 0) ? cout << m << " is even." : cout << m<< " is odd.";
  
return 0;
}


using if else

using namespace std;

int main()
{
int m;

cout << "Enter an integer: ";
cin >> n;

if ( m % 2 == 0)
cout << m << " is even.";
else
cout << m << " is odd.";

return 0;
}