! ARITHMETICS
! The following program calculates and prints the roots of a quadratic equation.
!
! Exercise: compile and execute the program, test the results for different values
! of input coefficients, does it always produce correct numbers?
! 1. Select values which do not work and use debugger to find the cause of the
! run-time error by making the program stop at different locations in the
! source code and checking the values of the variables.
! 2. Modify program to trap the error. Hint: Use IF
! 3. Modify program to compute more general solution. Hint: Use appropriate TYPE.
program quadratic_equation
! implicit declaration of variables is banned,
! all variables used in the program must be explicitly declared
implicit none
! declare all variables
! a, b, c: coefficients
! x1, x2: roots
! sqd, d: intermediates
real a, b, c, x1, x2, sqd, d
! Action!
! Read the coefficients
print*, "Enter a, the coefficient of x ** 2"
read*, a
print*, "Enter b, the coefficient of x"
read*, b
print*, "Enter c, the constant term"
read*, c
! Calculate the roots by the quadratic formula
d = b ** 2 - 4 * a * c
sqd = sqrt(d)
x1 = (-b + sqd) / (2 * a)
x2 = (-b - sqd) / (2 * a)
! Print the roots
print*, "The roots are"
print*, "x1 =", x1
print*, "x2 =", x2
end program quadratic_equation