Signup/Sign In
PUBLISHED ON: DECEMBER 26, 2022

JavaScript Program to Solve Quadratic Equation

Hello Everyone!

This tutorial will teach us how to write a program in JavaScript to solve a Quadratic Equation. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

This program computes the roots of a quadratic equation when its coefficients are known.

Quadratic Equations are of the form ax2 + bx + c = 0.

To find the roots (root1 and root2) of such an equation, we need to use the formula :

(root1,root2) = (-b ± sq(b2-4ac))/2

The term b2-4ac is known as the Discriminant [D] of a quadratic equation. It talks about the nature of the roots.

  • If the D is equal to 0, the roots are Real and Equal.
  • If the D is greater than 0, the roots are Real and Different.
  • If the D is less than 0, the roots are Complex and Different.

Program to Solve Quadratic Equation

// Write a code to solve Quadratic Equation in JavaScript

let r1, r2;

let a = prompt("Enter the first number: ");
let b = prompt("Enter the second number: ");
let c = prompt("Enter the third number: ");

// calculate discriminant
let D = b * b - 4 * a * c;

// condition for Real and Different roots
if (D > 0) {
    r1 = (-b + Math.sqrt(D)) / (2 * a);    r2 = (-b - Math.sqrt(D)) / (2 * a);
    console.log(`The roots of quadratic equation are ${r1} and ${r2}`);
}

// condition for Real and Equal roots
else if (D == 0) {
    r1 = r2 = -b / (2 * a);
    console.log(`The roots of quadratic equation are ${r1} and ${r2}`);
}

// if roots are not real
else 
    console.log(`The Roots are Complex and Different `);

When Discriminant >0


Enter the first number: 1
Enter the second number: 4
Enter the third number: 4
The roots of quadratic equation are 2 and -2

When Discriminant =0


Enter the first number: 1
Enter the second number: -6
Enter the third number: 9
The roots of quadratic equation are 3 and 3

When Discriminant < 0


Enter the first number: 1
Enter the second number: -3
Enter the third number: 10
The Roots are Complex and Different

Conclusion

In conclusion, we have learned how to write a JavaScript program to solve a quadratic equation. We discussed the general form of a quadratic equation and the formula used to solve it, and we walked through the steps of implementing this formula in JavaScript. We also learned about the importance of error handling and how to handle cases where the equation has no real solutions.

Solving a quadratic equation is a common task in mathematics and engineering, and being able to do so in JavaScript can be a valuable skill for any developer. I hope that this tutorial has provided you with a solid understanding of how to solve a quadratic equation in JavaScript and that you are now able to incorporate this functionality into your own programs and projects. Happy coding!

Take the first step towards becoming a JavaScript pro by signing up for our Interactive Course now!



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.