Sum and product of an array

From Rosetta Code
Revision as of 20:03, 3 December 2007 by rosettacode>Mwn3d (Added C++ example, cleaned up Java.)

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

C++

int main(int argc, char* argv[]){
  int list[]= {1,2,3,4,5};
  int sum= 0;
  long prod= 1;
  for(int i= 0;i < 5;i++){
    sum+= list[i];
    prod*= i;
  }
}


Java

public class SumProd{
  public static void main(String[] args){
    int sum= 0;
    int prod= 1
    int[] arg= {1,2,3,4,5};
    for (int i: arg) {
      sum+= i;
      prod*= i;
    }
  }
}