0

Why answer is " sum= 21"

#include <iostream> using namespace std; int main() { int array [7],sum=0; for(int i=0;i<7;i++) { array [i] = i; sum +=array[i]; } cout << "sum =" << sum; }

12th Dec 2016, 2:37 PM
Aman Khan
Aman Khan - avatar
6 ответов
+ 4
To explain this, look at the 'for' loop. In short, it means that 'i' , as well as number of arrays will become 0, then 1, then 2, then 3... and it stops once 'i' becomes 7. And you can see this 'sum += array[i];' , to simplify it, it just means this : sum = sum + i; Hence, if you go through the loop until 'i' becomes 7, sum will become " 0+1+2+3+4+5+6" which is 21.
12th Dec 2016, 2:46 PM
Wen Qin
Wen Qin - avatar
+ 3
sum initially is 0. in the 'for loop' we add each i value to sum until i=6 (i<7). that makes 0+1+2+....+6 = which is nothing but 21.
12th Dec 2016, 2:49 PM
P Sandesh Baliga
P Sandesh Baliga - avatar
0
can any body explain the code
12th Dec 2016, 2:39 PM
Aman Khan
Aman Khan - avatar
0
i can
12th Dec 2016, 2:40 PM
joooooooooshhhhhhhhh
joooooooooshhhhhhhhh - avatar
0
please tell me why the answer sum = 21
12th Dec 2016, 2:43 PM
Aman Khan
Aman Khan - avatar
0
thank you to explain
12th Dec 2016, 2:48 PM
Aman Khan
Aman Khan - avatar