Xeùt naêm nhuaàn Case.. Xeùt naêm nhuaàn kieåu if.[r]
(1)(Lưu Hành Nội Bộ) 1/ Những phép toán bản
Program toancoban; Var a,b,tong,hieu,tich: integer; thuong: real; Begin Writeln('nhap Vao Mot So Nguyen a Bat Ky');
Readln(a); Writeln('Nhap Vao Mot So Nguyen b Bat Ky');
Readln(b); Writeln('Tong Cua a Va b La:',a+b); Writeln('Hieu Cua a Va b La:',a-b); Writeln('Tich Cua a Va b La:',a*b); Writeln('Thuong Cua a Va b
La:',a/b:10:2); Readln; End.
2/ Xét năm nhuần Case. .of
program namnhuan; var x: integer; begin writeln('nhap vao mot so chi nam'); readln(x); case (x mod 4) of 0: writeln('nam',x,'la nam nhuan') else writeln('nam',x,'khong fai la nam nhuan'); end; readln;
end
3/ Xét năm nhuần kiểu if then program namnhuan; var x: integer; begin writeln('nhap vao mot so chi nam'); readln(x); if (x mod 4=0) then writeln('nam',x,'la nam nhuan') else writeln('nam',x,'khong fai la nam nhuan'); readln; end.
4/ Giải phương trình bậc 2
program PTB2; var a,b,c,Delta,x1,x2: real; begin writeln('nhap vao so bat ki'); readln(a,b,c); Delta:=b*b-4*a*c; if Delta<0 then writeln('phuong trinh vo nghiem'); if Delta=0 then writeln('phuong trinh co nghiem kep x1 = x2 =',b/2*a:10:2)
else begin writeln('phuong trinh co hai nghiem phan biet');
x1:=((-b)-(sqrt(Delta)))/2*a; x2:=(-b+sqrt(Delta))/2*a; end;
writeln('x1=',x1:10:2); writeln('x2=',x2:10:2); readln; end
5/ Giải phương trình bậc 1 program PTB1; var a,b: real; begin writeln('Nhap vao hai so bat ki'); readln(a,b); if a=0 then if b=0 then writeln('phuong trinh vo dinh') else writeln('phuong trinh vo nghiem') else writeln('phuong trinh co nghiem x =',-b/a:10:2);
readln; end
6/ Giá trị tuyệt đối program GTTD; var x,TD: integer; begin writeln('nhap vao mot so:'); readln(x); if x>0 then TD:=x else TD:=-x; writeln('Gia tri tuyet doi cua',x,'la:',TD); readln; end
7/ Hoán đổi số
(2)program hoandoi; var a,b,trgian: integer; begin writeln('nhap vao mot so nguyen a'); readln(a); writeln('nhap vao mot so nguyen b'); readln(b); if a<>b then begin trgian:=a; a:=b;
b:=trgian end;
writeln('hoan doi a cho b la: ',trgian); readln; end
8/ Min soá program min2so; var a,b,min: integer; begin writeln('nhap vao hai so nguyen:'); readln(a,b); if a<b then min:=a else min:=b; writeln('so nho nhat hai so la:',min); readln; end
9/ Min soá program min3so; var a,b,c,trgian: integer; begin writeln('nhap vao ba so nguyen bat ki'); readln(a,b,c); trgian:=a; if trgian>a then trgian:=b; if trgian>c then trgian:=c;
writeln('so nho nhat la:',trgian); readln; end
10/ Max soá
program max3so; var a,b,c,trgian: integer; begin writeln('- Nhap vao so nguyen bat ki'); readln(a,b,c); trgian:=a; if trgian<b then trgian:=b; if trgian<c then trgian:=c; writeln('- So lon nhat chinh la:',trgian); readln; end
11/ Max soá
program max2so; var a,b: integer; begin writeln('- Nhap vao so nguyen bat ki'); readln(a,b); if a>b then writeln('so lon nhat la:',a) else writeln('so lon nhat la:',b); readln; end
12/ Bốn mùa
Program bonmua; var ch: char; begin writeln('nhap vao mot tu chi thang'); readln(ch); case (ch) of
'x': writeln('day la mua xuan'); 'h': writeln('day la mua ha'); 't': writeln('day la mua thu'); 'd': writeln('day la mua dong'); else begin writeln('khong co mua nay'); writeln('xin ban hay vui long thu lai'); end; end; readln; end.
13/ Xeùt ba cạnh tam giác
program xet3canhtamgiac; var a,b,c,trgian: real; begin writeln('nhap vao canh tam giac'); readln(a,b,c); if a>b then begin trgian:=a;a:=b;b:=trgian end;
if b>c then begin trgian:=b;b:=c;c:=trgian end;
if a>b then begin trgian:=a;a:=b;b:=trgian end;
if (a>0) and (c<a+b) then writeln('day la ba canh cua tam giac') else writeln('day khong fai la ba canh tam giac');