Jump to content

Sum and product of an array: Difference between revisions

→‎{{header|UNIX Shell}}: Use an actual array
(Sum and Product of an Array in Rapira)
(→‎{{header|UNIX Shell}}: Use an actual array)
Line 3,002:
echo $sum $prod</lang>
 
{{works with|GNUBourne bash|3.2.0(1)-releaseAgain (i386-unknown-freebsd6.1)SHell}}
{{works with|Korn Shell}}
From variable:
{{works with|Zsh}}
Using an actual array variable:
 
<lang bash>LISTlist='(20 20 2');
SUM(( sum=0;, PRODprod=1; ))
for in in "$LIST{list[@]}"; do
SUM=$[$SUM +(( $i];sum PROD+=$[$PROD n, prod *= n $i];))
done;
printf '%d\t%d\n' "$sum" "$prod"
echo $SUM $PROD</lang>
</lang>
 
{{Out}}
<pre>42 800</pre>
 
=={{header|UnixPipes}}==
1,480

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.