The following program reads integer values from the keyboard. All values input (
ID: 3789313 • Letter: T
Question
The following program reads integer values from the keyboard. All values input (except the sentinel value which is -1) are greater than or equal to 1. The program calculates two sums: the sum of all the even values read in and the sum of all the odd values read in. These sums are printed at the screen.
A partial FORTRAN 90 program to accomplish the goals of this problem follows: lab 2(b) solution by KSTUDENT NAME> program lab2-b integer val, sumodd 0,sumEven 0 print lab 20 b) solution by KSTUDENT NAME> print enter integers greater than or equal to 1 print "Enter value -1 to end do print Enter val -1 to end enddo print end program lExplanation / Answer
#include <iostream>
using namespace std;
int main() {
int sum_even=0;//initialisation of variable to zero
int sum_odd=0;
int n;
cin>>n;
while(n){//if n is false then stop execution
if(n%2==0)
sum_even=sum_even+n;
else
sum_odd=sum_odd+n;
cin>>n;
}
cout<<"Even Sum is "<<sum_even<<endl;
cout<<"Odd Sum is "<<sum_odd<<endl;//print sum
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.