Tin học Pascal: Viết lệnh tự động in ra màn hình các số nguyên tố từ 0 đến 30. 19/07/2021 By Kaylee Pascal: Viết lệnh tự động in ra màn hình các số nguyên tố từ 0 đến 30.
Program abc; uses crt; var i,n,s : integer; begin clrscr; write(‘Cac so nguyen to tu 0 den 30 la: ‘); for i:=2 to 30 do begin s:=0; for n:=1 to i do if i mod n=0 then s:=s+1; if s=2 then write(i,’ ‘); end; readln; end. Trả lời
Dùng hàm: program in_nguyeto; uses crt; var i:byte; function ktnt(k:byte):boolean; var l:byte; begin ktnt:=false; if k<2 then exit; for l:=2 to trunc(sqrt(k)) do if k mod l=0 then exit; ktnt:=true; end; BEGIN clrscr; write(‘Cac so nguyen to tu 0 den 30 la: ‘); for i:=0 to 3 do if ktnt(i) then write(i,’ ‘); readln; end. Dùng chương trình thường: program in_nguyento; uses crt; var i,j,d:byte; begin clrscr; write(‘Cac so nguyen to tu 0 den 30 la: ‘); for i:=0 to 30 do if i>=2 then begin d:=0; for j:=1 to i do if i mod j=0 then d:=d+1; if d=2 then write(i,’ ‘); end; readln; end. Trả lời