+ 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.
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.
+ 5
I guess that its a for with empty code or inmediatly ';', so its empty, and shows 6.
+ 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.
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