How to multiply all the elements of an array in Pascal

1 Answer

0 votes
program MultiplyArrayElements;

var
  arr: array[1..5] of Integer; 
  i: Integer;
  product: Int64; // Use Int64 to handle larger results

begin
  // Initialize the array
  arr[1] := 2;
  arr[2] := 3;
  arr[3] := 4;
  arr[4] := 5;
  arr[5] := 6;

  // Initialize the product to 1
  product := 1;

  // Multiply all elements of the array
  for i := Low(arr) to High(arr) do
    product := product * arr[i];

  WriteLn('The product of all elements in the array is: ', product);
end.



(*
run:

The product of all elements in the array is: 720

*)

 



answered Jun 20, 2025 by avibootz
...