+ 1

Why does this code give me 10 as the output when I input 2?

#include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; return 0; }

10th Jan 2017, 1:10 AM
Piano Theorist
Piano Theorist - avatar
2 Antworten
+ 2
becuase after you input two, it adds 2 to your total 5 times. you input 2 2+2+2+2+2=10
10th Jan 2017, 2:41 AM
YaySushi
+ 1
Oh thank you! !
10th Jan 2017, 2:41 AM
Piano Theorist
Piano Theorist - avatar