0

int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << tota

I can't understand this 😑 why is the output 10 when the input is 2?

2nd Dec 2017, 7:27 PM
Mitra
Mitra - avatar
2 Réponses
+ 2
If you keep inputting 2, you will get 10. This is because the loop runs 5 times, and it adds 2 to total five times. Iteration trace (num: total) 1:2 2:4 3:6 4:8 5:10
2nd Dec 2017, 7:52 PM
Echo One
Echo One - avatar
0
thanks it's much better now
2nd Dec 2017, 7:59 PM
Mitra
Mitra - avatar