Quadratic equation in Pascal
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
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
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.