Nhập vào một mảng số nguyên và in ra các phần tử là số nguyên tố có trong mảng.

2 câu trả lời

uses crt;
var i,n:longint;
mang: array[1..9999999] of longint;
begin
clrscr;
write ('Nhap vao so phan tu co trong mang: '); readln (n);
for i:=1 to n do
begin
write('Nhap vao so nguyen thu ',i,' : ');readln(mang[i]);
end;
writeln ('Cac phan tu la so nguyen co trong mang la:');
for i:=1 to n do
begin
write(mang[i],' ');
end;
readln;
end.

Code:

var a:array[1..100000000] of longint;
i,n,j:longint;
function nt(x:longint):boolean;
begin
nt:=true;
if x<2 then nt:=false;
if x=2 then nt:=true;
if x>2 then
for j:=2 to trunc(sqrt(x)) do
if x mod j=0 then nt:=false;
end;
begin
read(n);
for i:=1 to n do read(a[i]);
for i:=1 to n do
if nt(a[i]) then write(a[i],' ');
end.

Câu hỏi trong lớp Xem thêm