Viết chương trình nhập vào mảng A gồm n phần tử và kiểm tra trong các phần tử , phần tử nào là số nguyên tố, Dufng Pascal nhe

2 câu trả lời

uses crt;
var a : array[1..100] of integer;
i,n : integer;
function snt(x : integer) : boolean;
var i,d : integer;
begin d:=0;
for i:=1 to x do if x mod i = 0 then d:=d+1;
if d=2 then snt:=true else snt:=false; end;
begin clrscr;
write('Nhap so phan tu :'); readln(n);
for i:=1 to n do begin
write('Nhap a[',i,']='); readln(a[i]);
end;
writeln('Cac phan tu la so nguyen to trong mang :');
for i:=1 to n do if snt(a[i])=true then write(a[i]:3);
readln
end.

uses crt;
var i,n,dem:longint; a:array[1..1000000]of longint;
function nt(a:longint):boolean;
var i:longint;
begin
   i:=2;
   while(a>1)and(a mod i<>0) do inc(i);
   nt:=i=a;
end;
begin
clrscr;
   write('N=');readln(n);
   for i:=1 to n do
      begin
         write('a[',i,']=');readln(a[i]);
         if nt(a[i]) then inc(dem);
      end;
   if dem=0 then writeln('Khong co phan tu la so nguyen to trong mang.') else
   writeln('Cac so nguyen to: ');
   for i:=1 to n do if nt(a[i]) then write(a[i],' ');
readln
end.

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