Viết chương trình: Nhập vào dãy N số nguyên (N <= 100). Tìm các số nguyên tố xuất hiện trong N số đã nhập. Biết rằng, số nguyên tố là số chỉ có ước là 1 và chính nó. Ví dụ: Input Output 7 4 3 11 3 3 7 11

2 câu trả lời

uses crt;

Var n,i,j:integer;

      a:array[1..100] of integer;

      kt:boolean;

begin

 write('nhap n: ');readln(n);

 for i:=1 to n do 

   begin

     write('nhap pt thu ',i,' : ');readln(a[i]);

   end;

for i:=1 to n do 

  begin

    kt:=true;

    for j:=2 to round(sqrt(a[i])) do if a[i] mod j=0 then kt:=false;//kt a[i] co phai so nt ko thi kt=false

    if kt=true then write(a[i],' ');//kt=true xuat

  end;

readln;

end.

Nếu sai nói để sửa

Const Fi='NTO.INP';
      Fo='NTO.OUT';

Var A: array[1..1000] of longint;
    n,i,j,k: longint;
    f,g: text;

Function ngto(a: longint): boolean;
Var i: longint;
Begin
        If a<2 then exit(false);
        For i:=2 to a div 2 do
                If a mod i = 0 then exit(false);
        Exit(true);
End;

Begin
        Assign(f,Fi); Reset(f);
        Assign(g,Fo); Rewrite(g);
        n:=0;
        While not eof(f) do
        Begin
                inc(n);
                Read(f,A[i]);
        End;
        For i:=1 to n do
                Readln(f,A[i]);

        While i<=n do
        Begin
                j:=1;
                While A[j]<>A[i] do
                        inc(j);
                If j<i then
                Begin
                        For k:=i to n-1 do
                                A[k]:=A[k+1];
                        n:=n-1;
                End
                Else inc(i);
        End;

        For i:=1 to n do
                If ngto(A[i]) then Write(g,A[i],' ');
        Close(f);
        Close(g);
End.