int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << tota | SoloLearn: Learn to code for FREE!

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?

12/2/2017 7:27:29 PM

Mitra

2 Answers

New Answer

+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

0

thanks it's much better now