Today Fortran is mainly used for numerical computation. This very simple example illustrates the basic program structure to solve quadratic equations:
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