in c++: Write a loop that reads positive integers from standard input and that t
ID: 2083035 • Letter: I
Question
in c++:
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.
exmaple:
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. Declare any variables that are needed.
answer:
int sum=0;
int num=1;
while(num > 0){
cin >> num;
if ((num % 2)==0 & (num>0)){
sum+=num;
}
}
cout << sum;
Explanation / Answer
#include<iostream>
using namespace std;
int main(){
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;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.