0
Please explain this.
int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl;
3 Respostas
+ 3
You can add a line between total += number and num++
cout << "iteration: " << num << ", total:" << total << endl;
Then you should see how it works.
+ 2
when num=1:
Let's assume user enter the number value as 5
total=total+number i.e. total=0+5=5
when num=2
Let's assume user enter the number value as 6
total=total+number i.e. total=5+6=11
when num=3
Let's assume user enter the number value as 7
total=total+number i.e. total=11+7=18
when num=4
Let's assume user enter the number value as 8
total=total+number i.e. total=18+8=26
when num=5
Let's assume user enter the number value as 9
total=total+number i.e. total=26+9=35
+ 1
Thanks a lot, Gordon 😊😊😊😊