How to rotate array elements left N times in Pascal

1 Answer

0 votes
program RotateArray;

const
  N = 7;

var
  arr: array[1..N] of Integer;
  i, temp1, temp2: Integer;

procedure PrintArray;
begin
  for i := 1 to N do
    Write(arr[i], ' ');
  Writeln;
end;

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

  // Print original array 
  PrintArray;

  // Rotate left by 2 positions 
  temp1 := arr[1];
  temp2 := arr[2];
  for i := 1 to N - 2 do
    arr[i] := arr[i + 2];
  arr[N - 1] := temp1;
  arr[N] := temp2;

  // Print rotated array 
  PrintArray;
end.



(*
run:

1 2 3 4 5 6 7 
3 4 5 6 7 1 2 

*)


 



answered Oct 22, 2025 by avibootz
edited Oct 22, 2025 by avibootz
...