While loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While loop

can anyone explain this to me #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; }

25th Mar 2018, 12:01 PM
sahir
sahir - avatar
2 Answers
0
Sure thing! Imagine you have a little program that's like a helpful friend with a calculator. It asks you nicely for five numbers, one by one, and as you give them, it adds them all up for you. Once you've shared all five numbers, it cheerfully tells you the total sum. It's like having a buddy who's great at quick math!
18th Apr 2024, 12:31 AM
Sofia
Sofia - avatar
0
1. Includes the input/output stream library. 2. Uses the standard namespace. 3. Starts the main function. 4. Initializes a variable `num` to 1. 5. Declares a variable `number`. 6. Initializes a variable `total` to 0. 7. Executes a loop while `num` is less than or equal to 5. 8. Reads an integer from the user and stores it in `number`. 9. Adds the value of `number` to `total`. 10. Increments the value of `num`. 11. Prints the total sum. 12. Indicates successful program execution.
18th Apr 2024, 12:33 AM
Sofia
Sofia - avatar