An algorithm to find the root of quadratic equation.

Solution :

The Quadratic  ax2 + bx + c.

where “a“, “b“, and “c” are just numbers; they are the “numerical coefficients” of the quadratic equation

The term b2-4ac is known as the determinant of a quadratic equation.

The determinant tells the nature of the roots.

  • If determinant is greater than 0, the roots are real and different.
  • If determinant is equal to 0, the roots are real and equal.
  • If determinant is less than 0, the roots are complex and different
  1. Start
  2. Enter the value of a, b and c.
  3. Calculate determinant as D= b2-4ac
  4. If (D> 0) then continue else ‘GOTO Step 9
  5. Calculate root1= -b + √D/2a
  6. Calculate root2= -b – √D/2a
  7. Print value of root1 and root2 these are Real and different
  8. GOTO END
  9. If (D==0) then continue else GOTO Step 13
  10. Calculate root1 = root2 = -b/2*a
  11. Print value of root1 and root2 the roots are real and equal.
  12. GOTO END
  13. Calculate RealPart = -b/2a

14.Calculate   imaginaryPart = √(-D)/2a

  1. Print value of root1= RealPart + i imaginaryPart
  2. Print value of root2= RealPart – i imaginaryPart the roots are complex and different

17.END

Flowchart to find the root of quadratic equation.

Flowchart to find root of quadratic equation
Flowchart to find root of quadratic equation

C-Code to find the root of quadratic equation using nested if …. else statement

#include <stdio.h>
#include <math.h>
int main()
{
             double a, b, c, D, root1, root2, realPt, imaginaryPt;
             printf("Enter coefficients a, b and c: ");
             scanf("%lf %lf %lf",&a, &b, &c);
             D = b*b-4*a*c;
             // condition for real and different roots
             if (D > 0)
             {
           printf(" \nThe roots are real and different.");
           root1 = (-b+sqrt(D))/(2*a);    // sqrt() function returns square root
           root2 = (-b-sqrt(D))/(2*a);
printf("\nroot1 = %.2lf and root2 = %.2lf",root1 , root2);
             }
             //condition for real and equal roots
             else if (D == 0)
             {
            printf(" \n The roots are real and equal.");
                        root1 = root2 = -b/(2*a);
printf("\n root1 = root2 = %.2lf;", root1);
             }
             // if roots are not real
             else
             {
            printf(" \n the roots are complex and different.");
                        realPt = -b/(2*a);
                        imaginaryPt = sqrt(-D)/(2*a);
       printf("\nroot1 = %.2lf+i %.2lf and root2 = %.2f- i %.2f", realPt, imaginaryPt, realPt, imaginaryPt);
             }
             return 0;
}