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; }
5 Respuestas
+ 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
+ 1
while num is < 5 or =5
get a new number
add it to total
num= num+1
+ 1
which part
0
thanks for quick response,
but i m still confused
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