Viết chương trình tìm nghiệm của phương trình bậc 2: ax² + bx + c=0 với a≠0
2 câu trả lời
Program ptb2;
uses crt;
var a,b,c,D,x1,x2:real;
begin
Clrscr;
Write(‘a,b,c:’); readln(a,b,c);
D:=b*b-4*a*c
If a=0 then write(‘pt tro thanh pt bac 1);
If b=0 then
If c=0 then write(‘pt vo so nghiem’);
Else write (‘pt vo nghiem’)
Else write(‘pt co nghiem=‘,-c/b)
Else
Begin;
If D<0 then write(‘pt vo nghiem’)
Else
Begin;
x1=(-b-sqrt(D))/(2*a);
x2=(-b+sqrt(D))/(2*a);
Writeln(‘x1=‘,x1:6:3;’x2=‘,x2:6:3);
End;
Readln;
end.
program giaiptbac2;
uses crt;
var a, b, c, d, x, x1, x2: real;
begin
clrscr;
write('Nhap 3 so a, b, c'); readln(a,b,c);
if a = 0 then write('Phuong trinh khong thoa man dieu kien')
else
begin
d:=b*b-4*a*c;
if d < 0 then write ('Phuong trinh vo nghiem');
if d = 0 then
begin
x:=(-b)/(2*a);
write('Phuong trinh co nghiem kep x1=x2=',x:10:2);
end;
if d > 0 then
begin
x1:=(-b+sqrt(d))/(2*a);
x2:=(-b-sqrt(d))/(2*a);
write('Phuong trinh co 2 nghiem');
write('x1=',x1:10:2);
write('x2=',x2:10:2);
end;
end;
Readln
End.