//printing the first even integers and adding the integers// #include#includeusi
ID: 3657617 • Letter: #
Question
//printing the first even integers and adding the integers// #include#includeusing namespace std; int main () { int a; int r; r = 2; a = 2*r; int sum; sum = 0; for(a = 2*r ; r<=10; a++) { sum = sum + 2*r;........................line(p) cout<<" "<<2*r<<endl; r++; } cout<<"The sum of the numbers" <<" " <<sum<<endl; return 0; } Q1:- My question is if i write the( sum = sum + 2*r;) line after r++, i get a different/incorrect sum but when i put the sum = sum + 2*r in line p before printing the numbers, the sum seems to be correct. The question is why the sum changes if i shift it below the r++)Explanation / Answer
that is because r++ is incrementing r by one so before r++, sum = sum + 2*r; after r++ sum's value would be: sum = sum + 2*(r+1);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.