{1,1,1,-2,1,2} How acn I multiply the elements in the list using loops?
$\begingroup$
$\endgroup$
1
I wouldn't use a loop here, I would go with:
Times @@ {1, 1, 1, -2, 1, 2}
But if you really need a procedural approach use a For
loop instead (even if it's probably not a good idea).
-
$\begingroup$ Obligatory comment: Why should I avoid the For loop in Mathematica? $\endgroup$ – MarcoB Dec 30 '19 at 21:13
$\begingroup$
$\endgroup$
t = 1; Do[t *= elem, {elem, {1, 1, 1, -2, 1, 2}}]
t
{* -4 *)
Fold[Times, {1, 1, 1, -2, 1, 2}]
$\endgroup$ – Bob Hanlon Dec 26 '19 at 17:55