+ 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; }
2 Antworten
+ 4
Summation of 5 numbers that we input each time in loop.
The sum will be stored in variable total.
+ 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