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.
.
.
Click on the title for the solution
.
.
int num=0;
int sumeven=0;
int sumodd=0;
int evencount=0;
int oddcount=0;
do
{
cin >> num;
if (num % 2 == 0 && num > 0)
{
evencount++;
sumeven += num;
}
else if (num > 0)
{
oddcount++;
sumodd += num;
}
}
while (num > 0);
cout<<sumeven;
cout<<" "<<sumodd;
cout<<" "<<evencount;
cout<<" "<<oddcount;
.
Click on the title for the solution
.
.
This is the answer:
:
int num=0;
int sumeven=0;
int sumodd=0;
int evencount=0;
int oddcount=0;
do
{
cin >> num;
if (num % 2 == 0 && num > 0)
{
evencount++;
sumeven += num;
}
else if (num > 0)
{
oddcount++;
sumodd += num;
}
}
while (num > 0);
cout<<sumeven;
cout<<" "<<sumodd;
cout<<" "<<evencount;
cout<<" "<<oddcount;
Comments
Post a Comment