PPT Slide
real :: A, B, C, Part_1, Part_2, Discriminant, First_Root
read (unit = *, fmt = *) A, B, C
write (unit = *, fmt = *) " Coefficients are: ", A, B, C
! A is not zero. Find discriminant.
Discriminant = B ** 2 - 4.0 * A * C
if (Discriminant >= 0.0) then ! .. Roots are real.
! .. For best results, force sign of Part_2 to agree with sign of Part_1.
Part_2 = sign( sqrt( Discriminant ) / (2.0 * A), Part_1 )
First_Root = Part_1 + Part_2
! .. For best results, unless First_Root is zero, compute second root as
! C / (A * First_Root) rather than as Part_1 - Part_2.
if (First_Root /= 0.0) then
write (unit = *, fmt = *) " Real roots: ", First_Root, C / (A * First_Root)
write (unit = *, fmt = *) " Real roots: ", First_Root, Part_1 - Part_2
else ! .. Discriminant < 0; roots are complex.
C_Root = (-B + sqrt( cmplx( Discriminant ))) / (2.0 * A)
write (unit = *, fmt = *) " Complex roots: ", C_Root, conjg( C_Root )
else ! .. A is zero. Equation is not a full quadratic.
write (unit = *, fmt = *) " Not a quadratic. Root is: ", -C / B
write (unit = *, fmt = *) " Equation has the form C = 0; C is: ", C