+ 1

What is wrong with this exercise?

What is the output ? int y; for (y=2;y<=6;y+=2) cout <<y; My answer is 246, but the SoloLearn program was accepting only a single number. This must be a mistake.

5th Jan 2017, 11:38 AM
abc
abc - avatar
4 ответов
+ 5
No, it's nothing wrong. If 2 is less then 6 then 2 + 2 This loops untill y is no longer less or is equal to 6 2+2+2 = 6 y+=2 operator means basically y = y + 2 Hope this helps.
5th Jan 2017, 11:45 AM
Alex
Alex - avatar
+ 5
I guess that its a for with empty code or inmediatly ';', so its empty, and shows 6.
5th Jan 2017, 12:16 PM
Nahuel
Nahuel - avatar
+ 3
Maybe you have "{};" after the for? That means that the for loops till the end, then the cout is executed only once, after the for loop has finished.
5th Jan 2017, 11:43 AM
Álvaro
0
correct answer 246 if you use this cout <<y <<endl; output : 2 4 6 if you want sum int sum+=y; cout <<sum; output : 12
5th Jan 2017, 6:34 PM
ASNM
ASNM - avatar