Quadratic equation in Fortran
Example for versions
gfortran 4.5.0
This example uses built-in complex
data type.
program Quadratic
integer a, b, c
real d, p1, p2
complex cp2
read (*, *) a
if (a.eq.0) then
write (*, *) "Not a quadratic equation"
stop
end if
read (*, *) b
read (*, *) c
d = b * b - 4 * a * c
p1 = - b / 2.0 / a
if (abs(d) < 1.0e-9) then
write (*, "(A, F8.3)") "x = ", p1
elseif (d > 0) then
p2 = sqrt(d) / 2.0 / a
write (*, "(2(A, F8.3))") "x1 = ", p1 + p2, char(13)//char(10)//"x2 = ", p1 - p2
else
cp2 = sqrt(cmplx(d)) / 2.0 / a
write (*, "(2(A, F8.3, F8.3), A)") "x1 = (", p1 + cp2, ")"//char(13)//char(10)//"x2 = (", p1 - cp2, ")"
end if
end
Comments
]]>blog comments powered by Disqus
]]>