Tuesday, May 7, 2013

How to calculate roots of Quadratic Equation using Javascript

0 comments
Quadratic Equations are of the form ax2 + bx + c = 0. To find roots(root1 and root2) of such an equation, we need to use the formula:
how to find Roots of Quadratic Equation: JavaScript
Roots of Quadratic Equation

Javascript code for calculating roots of quadratic equation is given below:

 <html>
<title>Root calculator</title>
<head>
    <script type="text/javascript">
    function roots()
    {
        var a = prompt("Enter value of a","");
        var b = prompt("Enter value of b","");
        var c = prompt("Enter value of c","");

        var root_part = Math.sqrt(b * b - 4 * a * c);
        var denom = 2 * a;

        var root1 = ( -b + root_part ) / denom;
        var root2 = ( -b - root_part ) / denom;

        document.write("1st root: "+root1+"<br />");
        document.write("2nd root: "+root2+"<br />");
    }
    </script>
    </head>
<body>
    <input type="button" onclick="roots()" value ="Root calculator" >
    </body>
</html>
 Demo for roots of Quadratic Equation using Javascript:
  • click the Root calculator button
  • simply enter the values of a, b & c
  • calculated roots will be shown

Leave a Reply

 
How To 99 © 2013 How to 99 &

How to Tutorial in 99 Categories