Sum and product of an array

From Rosetta Code
Revision as of 18:02, 3 December 2007 by rosettacode>Mwn3d (Started page, added Java example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Computer the sum and product of an arbitrary array of integers (hard code the array).

Java

int sum= 0;
int prod= 1
int[] arg= { 1,2,3,4,5 };
for (int i: arg) {
  sum+= i;
  prod*= i;
}