kiểm tra n có phải là số nguyên tố không ?kiểm tra n có hoàn toàn hay không ?xuất ra màn hình các số nguyên tố từ 2 đến n

2 câu trả lời

Cau 1 :

uses crt;
var i,n,d:longint;
begin
  clrscr;
     readln(n);
     if n<2 then write ('no')
     else
     begin
     for i:= 1 to n do
     if n mod i=0 then d:=d+1;
     if d=2 then write('n la snt')
       else
          write('n ko phai la snt');
end;
readln;
end.

Cau 2 :

   readln(n);
   for i:= 1 to n-1 do
   if n mod i=0 then t:=t+1;
   if t =n then write('la so hoan hao');
readln;
end.

Program BTT;
Uses crt;
Var n,i: longint;

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;

Function shh(b: longint): boolean;
Var i,s: longint;
Begin
        s:=0;
        For i:=1 to b div 2 do
                If b mod i = 0 then s:=s+i;
        If s=b then exit(true);
                    exit(false);
End;

Begin
        Clrscr;
        Write('Nhap n: '); Readln(n);
        If ngto(n) then Write(n,' la so nguyen to')
        Else Write(n,' khong phai la so nguyen to');
        Writeln;
        If shh(n) then Write(n,' la so hoan hao')
        Else Write(n,' khong phai la so hoan hao');
        Writeln;
        Write('Cac so nguyen to tu 2 den n la: ');
        For i:=2 to n do
                If ngto(i) then Write(i,' ');
        Readln;
End.

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