+ 1

Can you explain this C++ code:

#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; }

29th May 2019, 9:55 AM
Moksh Haria
Moksh Haria - avatar
2 Antworten
+ 4
Summation of 5 numbers that we input each time in loop. The sum will be stored in variable total.
29th May 2019, 10:19 AM
Sachin Artani
Sachin Artani - avatar
+ 3
while num - - > 1 and its less than 5 which is true cin>>number; - - >take an input total+=number add the inputed number to total, which can also be re-written as total=total + number; num++; increases thef value of num, which is previously 1 to 2, num++ is the same as num=num+1; when num is greater than 5, the loop stop and go to the next, which is cout<< total << endl; the value stored in total will then be printed out
29th May 2019, 12:15 PM
✳AsterisK✳
✳AsterisK✳ - avatar