Dùng hàm:
program in_nguyeto;
uses crt;
var i:byte;
function ktnt(k:byte):boolean;
var l:byte;
begin
ktnt:=false;
if k<2 then exit;
for l:=2 to trunc(sqrt(k)) do
if k mod l=0 then exit;
ktnt:=true;
end;
BEGIN
clrscr;
write('Cac so nguyen to tu 0 den 30 la: ');
for i:=0 to 3 do
if ktnt(i) then write(i,' ');
readln;
end.
Dùng chương trình thường:
program in_nguyento;
uses crt;
var i,j,d:byte;
begin
clrscr;
write('Cac so nguyen to tu 0 den 30 la: ');
for i:=0 to 30 do
if i>=2 then
begin
d:=0;
for j:=1 to i do
if i mod j=0 then d:=d+1;
if d=2 then write(i,' ');
end;
readln;
end.