Skip to main content

Algorithm and flowchart: quadratic roots

Short answer

Compute discriminant D = b² - 4ac. If D > 0, real distinct roots: (-b ± √D) / (2a); if D = 0, repeated real root: -b / (2a); if D < 0, complex conjugates: real part -b/(2a), imag part ±√(-D)/(2a).

Draw a flowchart and algorithm to find roots of a quadratic equation (ax² + bx + c = 0). Include all three conditions of the determinant.

For ax² + bx + c = 0, the discriminant D = b² - 4ac decides the nature of the roots: D > 0 gives two real and distinct roots, D = 0 gives two real and equal roots, D < 0 gives two complex conjugate roots.

Algorithm

  1. Start
  2. Read a, b, c
  3. If a = 0, print "Not a quadratic equation" and go to step 11
  4. Calculate D = b*b - 4*a*c
  5. If D > 0, go to step 6; otherwise go to step 8
  6. Calculate r1 = (-b + sqrt(D)) / (2*a) and r2 = (-b - sqrt(D)) / (2*a)
  7. Print "Real and distinct roots", r1, r2; go to step 11
  8. If D = 0, calculate r = -b / (2*a), print "Real and equal roots", r, r; go to step 11
  9. Otherwise (D < 0) calculate re = -b / (2*a) and im = sqrt(-D) / (2*a)
  10. Print "Complex roots": re + i im and re - i im
  11. Stop

Flowchart

         ( Start )
             |
       / Read a, b, c /
             |
        < a = 0 ? >--Yes--> / Print "Not quadratic" / --+
             | No                                       |
     [ D = b*b - 4*a*c ]                                |
             |                                          |
        < D > 0 ? >--Yes--> [ r1=(-b+sqrt(D))/(2a) ]    |
             | No           [ r2=(-b-sqrt(D))/(2a) ]    |
             |              / Print r1, r2 /  ----------+
        < D = 0 ? >--Yes--> [ r = -b/(2a) ]             |
             | No           / Print r, r /  ------------+
     [ re = -b/(2a)  ]                                  |
     [ im = sqrt(-D)/(2a) ]                             |
     / Print re + i im, re - i im /                     |
             |                                          |
             +<-----------------------------------------+
             |
         ( Stop )

Example: a = 1, b = 2, c = 5 gives D = 4 - 20 = -16 < 0, so roots are -1 + 2i and -1 - 2i.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 2 questions →