Sum and product of an array: Difference between revisions
Content added Content deleted
(Started page, added Java example.) |
(Added C++ example, cleaned up Java.) |
||
Line 1: | Line 1: | ||
Computer the sum and product of an arbitrary array of integers (hard code the array). |
Computer the sum and product of an arbitrary array of integers (hard code the array). |
||
=={{header|C++}}== |
|||
int main(int argc, char* argv[]){ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
for(int i= 0;i < 5;i++){ |
|||
sum+= list[i]; |
|||
prod*= i; |
|||
} |
|||
} |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
public class SumProd{ |
|||
⚫ | |||
public static void main(String[] args){ |
|||
⚫ | |||
int sum= 0; |
|||
⚫ | |||
int prod= 1 |
|||
for (int i: arg) { |
|||
int[] arg= {1,2,3,4,5}; |
|||
for (int i: arg) { |
|||
sum+= i; |
|||
prod*= i; |
|||
} |
|||
} |
|||
} |
} |
Revision as of 20:03, 3 December 2007
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; } } }