Program HOC24;
var i,n,d: integer;
t: real;
function nt(b: longint): boolean;
var j: longint;
begin
nt:=true;
if (b=2) or (b=3) then exit;
nt:=false;
if (b=1) or (b mod 2=0) or (b mod 3=0) then exit;
j:=5;
while j<=trunc(sqrt(b)) do
begin
if (b mod j=0) or (b mod (j+2)=0) then exit;
j:=j+6;
end;
nt:=true;
end;
begin
write('Nhap n: '); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
t:=0; d:=0;
for i:=1 to n do if nt(a[i]) then
begin
t:=t+a[i];
d:=d+1;
end;
write('Trung binh cong cac so nguyen to trong day: ',t/d:1:2);
readln
end.
Cách 2:
uses crt;
var a:array[1..100]of integer;
i,n,t,dem,j,kt:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
dem:=0;
for i:=1 to n do
if a[i]>1 then
begin
kt:=0;
for j:=2 to a[i]-1 do
if a[i] mod j=0 then kt:=1;
if kt=0 then
begin
t:=t+a[i];
inc(dem);
end;
end;
writeln('Trung binh cong cac so nguyen to la: ',t/dem:4:2);
readln;
end.