Anonymous user
Sum and product of an array: Difference between revisions
Modula-3
(Modula-3) |
|||
Line 413:
(All these notations are equivalent)
=={{header|Modula-3}}==
<pre>
MODULE Sumprod EXPORTS Main;
FROM IO IMPORT Put;
FROM Fmt IMPORT Int;
VAR a := ARRAY [1..5] OF INTEGER {1, 2, 3, 4, 5};
VAR sum: INTEGER := 0;
VAR prod: INTEGER := 1;
BEGIN
FOR i := FIRST(a) TO LAST(a) DO
INC(sum, a[i]);
prod := prod * a[i];
END;
Put("Sum of array: " & Int(sum) & "\n");
Put("Product of array: " & Int(prod) & "\n");
END Sumprod.
</pre>
Output:
<pre>
Sum of array: 15
Product of array: 120
</pre>
=={{header|Objective-C}}==
|