program ArrayOperations;
const
MAX_SIZE = 100;
var
arr: array[1..MAX_SIZE] of integer;
n, i, sum, minPrime, consecutiveOddCount, consecutiveOddStart: integer;
isPrime: boolean;
procedure InputArray(var arr: array of integer; n: integer);
var
i: integer;
begin
writeln('Nhap vao ', n, ' phan tu cua mang:');
for i := 1 to n do
begin
write('arr[', i, '] = ');
readln(arr[i]);
end;
end;
procedure OutputArray(arr: array of integer; n: integer);
var
i: integer;
begin
writeln('Cac phan tu cua mang la:');
for i := 1 to n do
write(arr[i], ' ');
writeln;
end;
function IsPrime(num: integer): boolean;
var
i: integer;
begin
IsPrime := num > 1;
for i := 2 to trunc(sqrt(num)) do
if num mod i = 0 then
begin
IsPrime := False;
Exit;
end;
end;
begin
write('Nhap vao so luong phan tu cua mang (n < 100): ');
readln(n);
if (n <= 0) or (n >= 100) then
begin
writeln('So luong phan tu khong hop le.');
Exit;
end;
InputArray(arr, n);
// a) In ra cac phan tu la so nguyen to trong mang
writeln('a) Cac phan tu la so nguyen to trong mang:');
for i := 1 to n do
if IsPrime(arr[i]) then
write(arr[i], ' ');
writeln;
// b) Tinh tong cac so nguyen to trong mang
sum := 0;
for i := 1 to n do
if IsPrime(arr[i]) then
sum := sum + arr[i];
writeln('b) Tong cac so nguyen to trong mang la: ', sum);
// c) In ra so nguyen to nho nhat trong mang
minPrime := MaxInt;
for i := 1 to n do
if IsPrime(arr[i]) and (arr[i] < minPrime) then
minPrime := arr[i];
if minPrime = MaxInt then
writeln('c) Khong co so nguyen to trong mang.')
else
writeln('c) So nguyen to nho nhat trong mang la: ', minPrime);
// d) In ra cac so le lien tiep nhau nho hon n
writeln('d) Cac so le lien tiep nhau nho hon n:');
consecutiveOddCount := 0;
consecutiveOddStart := 0;
for i := 1 to n do
begin
if arr[i] mod 2 = 1 then
begin
if c then
consecutiveOddStart := i;
Inc(consecutiveOddCount);
end
else
begin
if consecutiveOddCount > 1 then
begin
for i := consecutiveOddStart to consecutiveOddStart + consecutiveOddCount - 1 do
write(arr[i], ' ');
writeln;
end;
consecutiveOddCount := 0;
end;
end;
if consecutiveOddCount > 1 then
begin
for i := consecutiveOddStart to consecutiveOddStart + consecutiveOddCount - 1 do
write(arr[i], ' ');
writeln;
end;
end.