+ 2

Wrong challenge answers

There was this question, several times in the challenges: int x = 1; for(int i=3; i<5; i++){ x += i; } cout << x; According to the computer, 8 is not the right answer. Is this a mistake?

18th Aug 2016, 8:25 AM
Stanislav Stoychev
Stanislav Stoychev - avatar
7 Answers
+ 9
@Kunene: You are almost right, but only, if i<=5. In the above code though we have i<5. Hence, it will run in 2 loops with x=4 and subsequently x=8.
18th Aug 2016, 12:22 PM
Tensa
Tensa - avatar
+ 8
If you compile the code above the result is '8', according to ideone. Check here: http://ideone.com/iMhH6m
18th Aug 2016, 9:57 AM
0xfz13
+ 3
So, basically, most people answering here say the example code outputs 8, including myself. I just copy pasted your example code on the playground and it says the output is 8, as well. Did you misremember the phrasing? Or is the answer wrong? Take a screenshot of the question if you see it again and send to Sololearn if you are right.
18th Aug 2016, 7:21 PM
TylerFrost
TylerFrost - avatar
+ 2
Yes, this is a mistake. 8 is the right answer.
19th Aug 2016, 8:26 AM
Tamas Szekffy
Tamas Szekffy - avatar
+ 1
yes ....the answer should be 8.......and nothing else....
28th Aug 2016, 9:36 AM
Harshit Gupta
Harshit Gupta - avatar
- 3
no it is not, first iteration i is 3, second iteration i is 4, third iteration i is 5 so the loop code is not executed
18th Aug 2016, 9:14 AM
Stanislav Stoychev
Stanislav Stoychev - avatar
- 10
This is no mistake, the answer is 13, this program loops three times, therefore x = 1 when loop is entered, x= 4 after first loop, x= 8 after second loop, x = 13 after third loop, making the final answer x=13
18th Aug 2016, 9:41 AM
Kunene
Kunene - avatar