+ 1
can anyone explain this code?
int arr[3]= {2,2,3}; int res =1; for (int i=0; i<3; i++) { res*= arr[i]; } cout<<res; what are the answer and explain in details. thanks ;) ps: edited
3 Answers
+ 10
first of all it has a error
res* arr[i];
is not a correct command,
if you want to multiply all elements then write it as
res *= arr[i]; /* this will give the multiplication
of all elements of that array */
so output will be 1*2*2*3 = 12
/* but if you want to do something else then update your question again with proper commands. */
+ 9
Line 1 : Declare an integer array.
Line 2 : Declare integer "res" to 1.
Line 3 to 5 : From index 0 to 2, res is multiplied with array element of index i. arr[0] is 2, arr[1] is 2, arr[2] is 3. So, res becomes 2*2*3 = 12.
Line 6 : res = 12 is printed.
But note that the code works if Line 4 is "res *= arr[i]" but not "res * arr[i]".
- 1
thx for the answer :)
sorry for the mistake.