二次方程

今天 Fortran 主要用于数值计算。这个非常简单的例子说明了求解二次方程的基本程序结构:

program quadratic
  !a comment

  !should be present in every separate program unit
  implicit none

  real::a, b, c
  real::discriminant
  real::x1, x2

  print *, "Enter the quadratic equation coefficients a, b and c:"
  read *, a, b, c

  discriminant = b**2 - 4*a*c

  if ( discriminant>0 ) then

    x1 = ( -b + sqrt(discriminant)) / (2 * a)
    x2 = ( -b - sqrt(discriminant)) / (2 * a)
    print *, "Real roots:"
    print *, x1, x2

    ! Comparison of floating point numbers for equality is often not recommended. 
    ! Here, it serves the purpose of illustrating the "else if" construct. 
  else if ( discriminant==0 ) then

    x1 = - b / (2 * a)
    print *, "Real root:"
    print *, x1
  else

    print *, "No real roots."
  end if
end program quadratic