## What is Quadratic Equation?

A quadratic equation is a type of polynomial equation of the form ax^2 + bx + c = 0, where a, b, and c are constants and x is a variable. Quadratic equations have two solutions, which are called the roots of the equation. The solutions can be real or complex numbers, depending on the values of the constants a, b, and c.

Quadratic equations are commonly used to model various phenomena in mathematics, physics, and engineering

## Create a Javascript program to solve Quadratic Equation

Here’s a JavaScript program that will solve a quadratic equation of the form ax^2 + bx + c = 0:

```
function solveQuadratic(a, b, c) {
let x1, x2;
let discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
console.log(`Roots are real and different.`);
console.log(`x1 = ${x1} , x2 = ${x2}`);
} else if (discriminant == 0) {
x1 = (-b) / (2 * a);
console.log(`Roots are real and same.`);
console.log(`x1 = x2 =${x1}`);
} else {
console.log(`Roots are complex and different.`);
}
}
solveQuadratic(1, -3, 2); // Roots are real and different. x1 = 2 , x2 = 1
```

## Now let’s understand the explanation of this program

The function first declares two variables, `x1`

and `x2`

, which will be used to store the roots of the equation if they exist.

Next, the function calculates the discriminant (b^2 – 4ac) and stores it in a variable called `discriminant`

. This value will be used to determine the type of roots that the equation has.

The function then enters a `if`

block, which checks if the discriminant is greater than 0. If this is the case, then the roots of the equation are real and different. The function calculates the roots using the formula (-b +- sqrt(discriminant)) / 2a, and stores the results in `x1`

and `x2`

. It then prints a message indicating that the roots are real and different, and prints the values of `x1`

and `x2`

.

If the discriminant is not greater than 0, then the function enters an else if block that checks if the discriminant is equal to 0. If this is the case, then the roots of the equation are real and the same. The function calculates the root using the formula -b / 2a, and stores the result in `x1`

. It then prints a message indicating that the roots are real and the same, and prints the value of `x1`

.

If the discriminant is not equal to 0, then the function enters the final `else`

block, which means that the roots of the equation are complex and different. It prints a message indicating this.

Finally, the function is called with the arguments `1`

, `-3`

, and `2`

, which corresponds to the equation x^2 – 3x + 2 = 0. The output is:

```
Roots are real and different.
x1 = 2 , x2 = 1
```

This indicates that the roots of the equation are 2 and 1.

- Off-page SEO techniques to improve SEO - June 13, 2023
- Javascript practice exercises for beginners - June 12, 2023
- How to find my blog on Google search - June 12, 2023