Write a loop that reads positive integers from standard input and that terminate
ID: 3653619 • Letter: W
Question
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.
Explanation / Answer
int num=0; int sum1=0; int sum2=0; int sumeven=0; int sumodd=0; int evencount=0; int oddcount=0; do { cin >> num; if (num % 2 == 0 && num > 0) { evencount++; sum1=num++; sumeven = sum1 + sumeven; } else if (num > 0) { oddcount++; sum2++; sumodd = sum2 + sumodd; } } while (num > 0); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.