kt n có phải là số nguyên tố hay không xuất ra màn hình cac số nguyên tố từ 2-n và tính tổng của chúng kt n có phải là số hoàn hảo không? xuất ra màn hình các số hoàn hảo chia hết cho cả 3 và 9? làm trên 1 chương trình giúp với ạ đăng nhiều lần rồi mà kh ai giúp hết

2 câu trả lời

Program BTT;
Uses crt;
Var n,i,t,d: 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);

        t:=0;
        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

                Begin

                        Write(i,' ');

                       t:=t+i;

               End;

       Writeln;

       Writeln('Tong cac so nguyen to la: ',t);

       d:=0;

       Write('Cac so hoan hao: ');
       For i:=1 to n do
       If (shh(i)) and (i mod 3=0) and (i mod 9=0) then inc(d);

       If d=0 then Write('Khong co')
       Else
       For i:=1 to n do
                If (shh(i)) and (i mod 3=0) and (i mod 9=0) then Write(i,' ');


       Readln;

End.

Program songuyento;
Uses crt;
Var n,i,t,d: 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);
t:=0;
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
Begin
Write(i,' ');
t:=t+i;
End;
Writeln;
Writeln('Tong cac so nguyen to la: ',t);
d:=0;
Write('Cac so hoan hao: ');

For i:=1 to n do
If (shh(i)) and (i mod 3=0) and (i mod 9=0) then inc(d);
If d=0 then Write('Khong co')
Else
For i:=1 to n do
If (shh(i)) and (i mod 3=0) and (i mod 9=0) then Write(i,' ');
Readln;
End.