Bài tập Pascal
n,I : integer; ok : boolean; Begin Vrite (‘nhập:’); Readln(n);If n <= 1 then ok:= FalseElseIf n = 2 then ok:= TrueElseBeginI:= 2;While (n mod I <> 0) and ( i<= sqrt(n));If i > sqrt(n) then ok:= trueElse ok:= false;End;If ok then write (‘ Đó là số nguyên tố’)Else write (‘ Đó không à số nguyên tố’);Readln; End . Var n,I,k : integer; Begin Vrite (‘nhập:’); Readln(n);I:= 2;K:= trunc(sqrt(n));RepeatInc(i);Until (i>k) or (n mod I = 0) ;If i > k then write (‘ Đó là số nguyên tố’)Else write (‘ Đó không à số nguyên tố’);Readln; End .Program Ktra_sngto; Var n: word; Funtion Ngto(k:word): Boolean;Var i,dem:Word; begindem:=0; Ngto:=false;for i:=2 to n do if n mod i = 0 themdem:=dem+1;if dem = 2 then Ngto:= true;end; BEGIN Writeln('Nhap n '); readln(n);while n <2 do beginwriteln('nhap lai');readln(n);end;if Ngto(n) then writeln(' So nguyen to')elsewriteln('Khong la so nguyen to;End. Sưu tầm và biên soạn: Nguyễn Minh Đức Bài tập Pascal Bài 2: Nhập 1 số tự nhiên n.Liệt kê các số nguyên tố nhỏ hơn n .VD: n=13Các số nguyên tố nhỏ hơn n: 2 ,3 ,5 , 7, 11 Program bt;Var i,n,j : integer; Begin Write ('Nhap n:');Readln(n);Write ('Cac so nguyen to nho hon ',n,' la:');For i:=2 to n-1 doBeginj:=2;While i mod j <>0 then j:=j+1;if i=j then write (i,',');end;readln; End . Bài 3 :Var n:longint;{------} Function ngto(n:longint):boolean;Var i:word;Beginngto:=false;for i:=2 to trunc(sqrt(n)) doif n mod i=0 then exit;ngto:=true;end;{------} BEGIN write('nhap n: ');readln(n);for i:=2 to n doif ngto(i) then write(i,' ');Readln; END . Bài 4: tinh tổng và tíchVar i,tong,tich,n,m:longint; Begin write('nhap so n,m: '); readln(n,m);tong:=0; tich:=1;for i:=1 to m dobegintong:=tong+(n mod 10);tich:=tich*(n mod 10);n:=n div 10;end;writeln('tong la: ',tong);writeln('tich la: ',tich);Readln; End . Sưu tầm và biên soạn: Nguyễn Minh Đức Bài tập Pascal Bài 5 : Nhập 1 số đếm các chứ số của số nhập vào.Var a,i,j,n:longint; Begin write('Nhap so n: ');readln(n);i:=0; j:=10;repeata:=n mod j;j:=j*10;i:=i+1;until a=n;write('So chu so cua n la: ',i);readln; EndBài 6. Đổi kí tự thành chữ hoa Program DoiChu;Var St:String;i: integer; Begin Write('Hãy nhập tên của bạn : '); Readln(St);FOR i:= 1 TO Length(St) DOSt[i] := Upcase(St[i]);(*Hàm Upcase đổi ký tự thành chữ in hoa*)Writeln;Writeln(St);Readln; End . Bài 7 : VCT tính số tiền điện trong 1 thắng.Var kw, SoTien:LongInt; Begin Write('Nhap so KW dien da su dung trong thang: ');Readln(kw); Case kw of 1..100: SoTien := kw*650;101..200: SoTien :=100*650+(kw-100)*1100;201..300: SoTien :=100*650+100*1100+(kw-200)*2000; Else SoTien :=100*650+100*1100+100*2000+(kw-300)*3000; End ;Writeln('So Tien phai tra trong thang la: ',SoTien);Readln; EndBài 8 :Viết chương trình kiểm tra trong 3 số a,b,c nhập từ bàn phím số nào là lớn nhất. uses crt;var a, b, c, max: integer; Begin clrscr;write ('Nhap so a:= '); readln (a);write ('Nhap so b:= '); readln (b);write ('Nhap so c:= '); readln (c);max:=a; {cái này dạng như chúng ta giả sử a là số lớn nhất :D }if max<b then max:=b;if max<c then max:=c;write ('so lon nhat max:= ',max);readln; End . Sưu tầm và biên soạn: Nguyễn Minh Đức |