Open In App

JavaScript Program to Solve Quadratic Equation

Last Updated : 24 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to solve Quadratic equations with the help of JavaScript, A quadratic equation is a polynomial equation of degree 2, represented as ax2 + bx + c = 0.

ax2 + bx + c = 0
where a, b and c are real numbers and a ≠ 0

A quadratic equation’s zeros, also known as roots, are the values of x that satisfy the equation when substituted, resulting in the left-hand side being equal to zero.

Roots of Quadratic Equation using Sridharacharya Formula

The roots could be found using the below formula (It is known as the formula of Sridharacharya)

x=\frac{-b\pm \sqrt{b^2-4ac}}{2a}

The values of the roots depend on the term (b2 – 4ac) which is known as the discriminant (D)

If D > 0:
        => This occurs when b2 > 4ac.
        => The roots are real and unequal.
        => The roots are {-b + √(b2 – 4ac)}/2a and {-b – √(b2 – 4ac)}/2a.
If D = 0:
        => This occurs when b2 = 4ac.
        => The roots are real and equal.
        => The roots are (-b/2a).
If D < 0:
        => This occurs when b2 < 4ac.
        => The roots are imaginary and unequal.
        => The discriminant can be written as (-1 * -D).
        => As D is negative, -D will be positive.
        => The roots are {-b ± √(-1*-D)} / 2a = {-b ± i√(-D)} / 2a = {-b ± i√-(b2 – 4ac)}/2a where i = √-1.

JavaScript Program to Solve Quadratic Equation using Sridharacharya Formula

Using the Sridharacharya formula to solve a quadratic equation with coefficients a, b, and c, finding the roots with positive or negative square roots of the discriminant.

Example: In this example, we are using the above-explained approach.

Javascript




// JavaScript program to find roots
// of a quadratic equation
  
// Prints roots of quadratic
// equation ax * 2 + bx + x
function findRoots(a, b, c) {
  
    // If a is 0, then equation is not
    // quadratic, but linear
    if (a == 0) {
        console.log("Invalid");
        return;
    }
  
    let d = b * b - 4 * a * c;
    let sqrt_val = Math.sqrt(Math.abs(d));
  
    if (d > 0) {
        console.log('Roots are real and different');
  
        console.log(
            (-b + sqrt_val) / (2 * a) + " and " +
            (-b - sqrt_val) / (2 * a)
        );
    }
    else if (d == 0) {
        console.log('Roots are real and same');
  
        console.log(-b / (2 * a) + " and " +
            -b / (2 * a));
    }
  
    // d < 0
    else {
        console.log('Roots are complex');
  
        console.log(-b / (2 * a) + " + i" +
            sqrt_val / (2 * a) + " and " +
            -b / (2 * a) + " - i" + sqrt_val) / (2 * a);
    }
}
  
// Driver Code
let a = 1, b = -7, c = 12;
  
// Function call
findRoots(a, b, c);


Output

Roots are real and different
4 and 3

JavaScript Program to Solve Quadratic Equation using the Custom function

In this approach, we create a custom function to calculate the roots without explicitly using the quadratic formula.

Example: In this example, The myResult(a, b, c) function calculates the discriminant of a quadratic equation with coefficients a, b, and c. Based on the discriminant, it finds the real or repeated roots

Javascript




function findRoots(a, b, c) {
    return b * b - 4 * a * c;
}
  
function myResult(a, b, c) {
    const d = findRoots(a, b, c);
  
    if (d > 0) {
        const root1 = (-b + Math.sqrt(d)) / (2 * a);
        const root2 = (-b - Math.sqrt(d)) / (2 * a);
        return [root1, root2];
    } else if (d === 0) {
        const root = -b / (2 * a);
        return [root];
    } else {
        return [];
    }
}
  
// Example usage:
let a = 1;
let b = -3;
let c = 2;
let result = myResult(a, b, c);
console.log("Roots:", result);


Output

Roots: [ 2, 1 ]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads