Quadratic equation in Pascal
Example for versions
Free Pascal 2.0.4,
Free Pascal 2.2.0,
Turbo Pascal 1.0,
Turbo Pascal 2.0,
Turbo Pascal 3.0,
Turbo Pascal 4.0,
Turbo Pascal 5.0,
Turbo Pascal 6.0,
gpc 20070904
Pascal has built-in complex data type complex
, but using it is inconvenient in this case, because writeln
can’t output complex numbers directly, and functions Re
and Im
would have to be used. In this example calculations are done in real numbers. Library function halt
(added in Extended Pascal) exits current block (in later versions it is replaced with exit
).
program Quadratic;
var
A,B,C,D: integer;
begin
write('A = ');
readln(A);
if (A=0) then
begin
writeln('Not a quadratic equation.');
halt;
end;
write('B = ');
readln(B);
write('C = ');
readln(C);
D := B*B-4*A*C;
if (D=0) then
begin
writeln('x = ',-B/2.0/A);
halt;
end;
if (D>0) then
begin
writeln('x1 = ',(-B+Sqrt(D))/2.0/A);
writeln('x2 = ',(-B-Sqrt(D))/2.0/A);
end
else
begin
writeln('x1 = (',-B/2.0/A,',',Sqrt(-D)/2.0/A,')');
writeln('x2 = (',-B/2.0/A,',',-Sqrt(-D)/2.0/A,')');
end;
end.
Comments
]]>blog comments powered by Disqus
]]>