Please explain this. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please explain this.

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

17th Dec 2018, 12:18 PM
Gazal Soni
Gazal Soni - avatar
3 Answers
+ 3
You can add a line between total += number and num++ cout << "iteration: " << num << ", total:" << total << endl; Then you should see how it works.
17th Dec 2018, 12:30 PM
Gordon
Gordon - avatar
+ 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
17th Dec 2018, 12:36 PM
Rishi Anand
Rishi Anand - avatar
+ 1
Thanks a lot, Gordon 😊😊😊😊
17th Dec 2018, 12:34 PM
Gazal Soni
Gazal Soni - avatar