C++ Program to Find all Square Roots of a Quadratic Equation

In this post, we will learn how to find all square roots of a quadratic equation using C++ Programming language. Let’s learn about quadratic equations and their roots.

A quadratic equation can be expressed as ax2 + bx + c = 0, where a, b and c are constants and a != 0.

To  calculate the roots of a quadratic equation, we use the following formula:

  • First Root = (-b + (d)½) / 2a
  • Second Root = (-b – (d)½) / 2a

Where, d is the discriminant such that d = b2 – 4ac.

We will use these formulas in this program to find the square roots of the quadratic equation.

So, without further ado, let’s begin this tutorial.

C++ Program to Find all Square Roots of a Quadratic Equation

// C++ Program to Find all Square Roots of a Quadratic Equation
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    float a, b, c, d, root1, root2, real, img;
    
    // Asking for input
    cout << "Enter the coefficients of a, b and c: ";
    cin >> a >> b >> c;
    
    // Calculating determinant
    d = b * b - 4 * a * c;
    
    // Calculating the roots
    if (d > 0){
        root1 = (-b + sqrt(d)) / (2 * a);
        root2 = (-b - sqrt(d)) / (2 * a);
        cout << "Roots of the quadratic equation are real and different: " << endl;
        cout << "First Root: " << root1 << endl;
        cout << "Second Root: " << root2 << endl;
    }
    
    else if (d == 0){
        root1 = root2 = -b / (2 * a);
        cout << "Roots of the quadratic equation are real and same: " << endl;
        cout << "First Root: " << root1 << endl;
        cout << "Second Root: " << root2 << endl;
    }
    
    else {
        real = -b / (2 * a);
        img = sqrt(-d) / (2 * a);
        cout << "Roots are different and imaginary: " << endl;
        cout << "x1 = " << real << "+" << img << "i" << endl;
        cout << "x2 = " << real << "-" << img << "i" << endl;
    }
    return 0;
}

Output

Enter the coefficients of a, b and c: 1 5 6
Roots of the quadratic equation are real and different: 
First Root: -2
Second Root: -3

How Does This Program Work ?

    float a, b, c, d, root1, root2, real, img;

In this program, we have declared eight floating data type variables named a, b, c, d, root1, root2, real and img.

    // Asking for input
    cout << "Enter the coefficients of a, b and c: ";
    cin >> a >> b >> c;

Now, the user is asked to enter the coefficients of the quadratic equation.

    // Calculating determinant
    d = b * b - 4 * a * c;

Discriminant is calculated using the formula d = b2 – 4ac.

    // Calculating the roots
    if (d > 0){
        root1 = (-b + sqrt(d)) / (2 * a);
        root2 = (-b - sqrt(d)) / (2 * a);
        cout << "Roots of the quadratic equation are real and different: " << endl;
        cout << "First Root: " << root1 << endl;
        cout << "Second Root: " << root2 << endl;
    }

Now, we check whether d > 0, d = 0 or d < 0.

If d > 0, then the roots are different and real. Its real roots are calculated using the formula,

  • First Root = (-b + (d)½) / 2a
  • Second Root = (-b – (d)½) / 2a
    else if (d == 0){
        root1 = root2 = -b / (2 * a);
        cout << "Roots of the quadratic equation are real and same: " << endl;
        cout << "First Root: " << root1 << endl;
        cout << "Second Root: " << root2 << endl;
    }

If d = 0, then it’s roots are real and equal. The equal roots are calculated using the formula:

  • First Root = Second Root = -b / 2a
    else {
        real = -b / (2 * a);
        img = sqrt(-d) / (2 * a);
        cout << "Roots are different and imaginary: " << endl;
        cout << "x1 = " << real << "+" << img << "i" << endl;
        cout << "x2 = " << real << "-" << img << "i" << endl;
    }

And if d < 0, then its roots are different and complex. And, the formula to calculate the roots are:

  • First Root = (-b/2a) + [i(-d)½ / 2a]
  • Second Root = (-b/2a) – [i(-d)½ / 2a]

Conclusion

I hope after going through this post, you understand how to find all square roots of a quadratic equation using C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *