The general form of the quadratic equation can be written as follows:

ax^2 + bx + c = 0

where a, b and c are known real numbers, called equation coefficients. In order to calculate the roots of this equation, or to find solutions, we will use generally known formulas:

\left\{x_1=\frac{-b+\sqrt{\Delta}}{2a}\\x_2=\frac{-b-\sqrt{\Delta}{2a}}\ \ \qquad \ \ \right\qquad\ \qquad\ \qquad\ for\ \Delta>0

or

x=\frac{-b}{2a}\ \ \qquad\ \qquad\ for\ \Delta=0

When \Delta<0
the equation has no solution.

\Delta=b^2-4ac

 

 alg square en

Implementation of the algorithm in Pascal

Implementation of the algotithm in C++

Implementation of the algorithm in Java

Description of the algorithm:

  1. Start - our algorithm starts here.
  2. We load input data - coefficients a, b and c of the equation.
  3. We check if the coefficient a of the equation is equal to 0.
  4. If a = 0 we deal with a linear equation, we invoke the algorithm of solving the linear equations.
  5. We calculate the delta value.
  6. We check if the delta is less than 0.
  7. If delta <0 we print information, the equation has no solutions.
  8. We check if the delta is equal to 0.
  9. If delta = 0, we calculate the value of one double solution x.
  10. When delta = 0, after calculating x, we write its value.
  11. We reach this point when none of the previous conditions have been met (delta <0 and delta = 0), i.e. when delta> 0. We calculate the values of two solutions of the equation: x1 and x2.
  12. We print the calculated values of both solutions.
  13. Stop - the end of the algorithm common to all roads.

 

   
© jtrzeciak@alg24.com