Quadratic Equation Solver

Programming

This program solves quadratic equations of the form ax² + bx + c = 0 using the quadratic formula.

Quadratic Formula

The roots are given by:

x = (-b ± √(b² - 4ac)) / (2a)

The discriminant D = b² - 4ac determines the nature of the roots:

Implementation

program quadratic_equation_solver
    implicit none
    integer, parameter :: dp = kind(1.0d0)
    real(dp) :: a, b, c, discriminant, root1, root2

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

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

    if (discriminant < 0.0) then
        print *, "The equation has no real roots."
    else
        root1 = (-b + sqrt(discriminant)) / (2.0 * a)
        root2 = (-b - sqrt(discriminant)) / (2.0 * a)
        print *, "The roots are: ", root1, " and ", root2
    end if
end program quadratic_equation_solver

Example Interaction

Enter coefficients a, b, and c:
1 -5 6
The roots are:    3.0000000000000000  and    2.0000000000000000