Roots of Quadratic Equation: JavaScript
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

Video Explaining The JavaScript Code To Find Roots of a Quadratic Equation:
To calculate the roots of a quadratic equation using a computer program, we need to break down the formula and calculate smaller parts of it and then combine to get the actual solution.
So lets calculate square root of b2 – 4 * a * c and store it in variable root_part. Also store 2 * a in variable denom. Now calculate ( – b + root_part ) / denom and store it in root1 and ( – b – root_part ) / denom in root2.
Output the values of root1 and root2 to the browser using document.write statement.
Javascript Coding Explained In Above Video:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<html>
<head><title>Quadratic Equation</title></head>
<body>
<script type="text/javascript">
<!--
var a = prompt("Enter value of a","1");
var b = prompt("Enter value of b","4");
var c = prompt("Enter value of c","4");
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>
</body>
</html> |
Usually people will make mistake in this line
var root_part = Math.sqrt(b * b - 4 * a * c);
Be sure that, you write capital letter M in Math.sqrt.
Work Space:
Quadratic Equation: ax2 + bx + c = 0
Let,
a = 1
b = 4
c = 4
i.e., 1x2 + 4x + 4 =0
=> 1x2 + 2x + 2x + 4 = 0
=> x ( x + 2 ) + 2 ( x + 2 ) = 0
=> ( x + 2 ) + ( x + 2 ) = 0
=> x + 2 = 0 AND x + 2 = 0
=> x = -2 AND x = -2
Related posts:
Get FREE blog updates to your email inbox. Enter your email ID and subscribe:
Start Making Money From Your Programming Skills
- Subscribe to Technotip.com blog update and you will be able to download "Tips, Tricks and Strategies to Make Money Online" eBook for free.
- You will also receive tips to improve your programming skills, and strategies to make money from your programming skills. We will also send useful resources for learning and building your application.
- You can also make money with us, by just recommending our website to your friends and family. You will get complete strategy for making $300 and more in the ebook that we will send you - once you subscribe to our free blog update using the below form..


Leave a Reply