-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2P1.html
More file actions
54 lines (46 loc) · 1.43 KB
/
L2P1.html
File metadata and controls
54 lines (46 loc) · 1.43 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript current day and time</title>
<h1>
This program computes roots of a quadratic equation when its coefficients are known.
The standard form of a quadratic equation is:
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
</h1>
</head>
<body>
<script type="text/javascript">
// program to solve quadratic equation
let root1, root2;
// take input from user
let a = prompt("Enter the first number: ");
let b = prompt("Enter the second number: ");
let c = prompt("Enter the third number: ");
// calculate discriminant
let discriminant = b * b - 4 * a * c;
// condition for real and different roots
if(discriminant > a) {
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
// result
console.log(`The roots of quadratic equation are ${root1} and ${root2}`);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
//result
console.log(`The roots of quadratic equation are ${root1} and ${root2}`);
}
// if roots are not real
else {
let realPart = (-b / (2 * a)).toFixed(2);
let imagPart = (Math.sqrt(-discriminant) / (2 * a)).toFixed(2);
// result
console.log(`The roots of quadratic equation are ${realPart} + ${imagPart}i and ${realPart} - ${imagPart}i`);
}
</script>
</body>
</html>