+ 1
What if the user entered values less than or more than 5 times ??
In the code below the user is supposed to enter values 5 times so that the code runs, but if ( the case above ) ^^ is True, What happens ? int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; }
6 Answers
+ 4
If the user enters 5 values(smaller or bigger doesn't matters), the following code will return their sum. If the user entered only one value. Then because it is a loop statement it will take that value again and again. For Example:-
If the input is 5. It will print 25.
+ 4
If users enters a values less than 5. For example:- 5 and 10.
It will go for a loop and first will add 5 to total and then 10. After that it will add the last entered number again till the loop ends.
Therefore the following input will print 45.
+ 4
Did you understand @Nacer?
+ 1
So it will take it as the user entered the value 5 five times and gives 25 as the sum ...
+ 1
aaah !! Got it ! Thanks a lot Akash Pal !! really helpful !
+ 1
The user can't put more then 5 numbers or less then 5 because that's how the code is written. Don't use the SoloLearn compiler it creates confusion get a desktop one