0

While loops

Still not able to understand, Please help!!! //c++ #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; }

16th Jun 2018, 3:09 PM
Aksh97
Aksh97 - avatar
5 Answers
+ 2
while value of num variable is less than or equal to 5 it will take input from and store it in number variable. Thenafter value of number will be added with value of total variable and num is incremented by 1(num++) It will result in output when while loop complete its execution that is when num got value greater than 5
16th Jun 2018, 3:48 PM
Rajan Pandey
Rajan Pandey - avatar
+ 1
while num is < 5 or =5 get a new number add it to total num= num+1
16th Jun 2018, 3:18 PM
‎ ‏‏‎Anonymous Guy
+ 1
which part
16th Jun 2018, 3:47 PM
‎ ‏‏‎Anonymous Guy
0
thanks for quick response, but i m still confused
16th Jun 2018, 3:39 PM
Aksh97
Aksh97 - avatar
0
finally i understood this; #include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; while(num<=5){ cin >> number; total+=number; cout << "Total = " << total << endl; num++; } return 0; } ====================================== basically this will happen inside it total + number = total ( if number is 3) then 0 + 3= 3 3+ 3 =6 6+3=9 9+3=12 12+3=15 (calculation will continue till num value equals 5) final total will be 15 ====================================== and i thought this should happen, num=num+1 3+1=4 4+1=5 5+1=6 6+1=7 7+1=8 total = 8 😅 just got confused between num and number
17th Jun 2018, 2:05 PM
Aksh97
Aksh97 - avatar