*Write a c++ program to find roots of quadratic equation in c++ using class.*

We will find the roots of the quadratic equation using the discriminant. Suppose a quadratic equation

The discriminant *D* of the above equation is

The value of determinant defines the nature of the roots.

- If the discriminant is greater than 0, then roots are real and different. The roots and of the quadratic equation are given by
- If the discriminant is equal to 0, then the roots are real and equal. The roots of the quadratic equation are given by
- If the discriminant is less than 0, then the roots are complex and different. The roots and of the quadratic equation are given by

We will implement the program by creating a class *QuadraticEquation*.

Data Members

- a: Coefficient of
- b: Coefficient of
- c: Constant

Member Function

- Roots: Function to display roots of the quadratic equation.

```
#include <iostream>
#include <cmath>
using namespace std;
class QuadraticEquation {
float a, b, c;
public:
QuadraticEquation() {
cout << "ax^2 + bx + c = 0" << endl;
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
cout << "Enter c: ";
cin >> c;
}
void Roots() {
float discriminant = b * b - 4 * a * c, real, imaginary, x1, x2;
if (a == 0) {
cout << "Roots" << endl;
cout << "x = " << -c << endl;
}
else if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different" << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and equal" << endl;
x1 = (-b + sqrt(discriminant)) / (2 * a);
cout << "x =" << x1 << endl;
}
else {
real = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << real << "+" << "i" << imaginary << endl;
cout << "x2 = " << real << "-" << "i" << imaginary << endl;
}
}
};
int main() {
QuadraticEquation qe;
qe.Roots();
}
```

**Output**

**Read**

Program to Print Diamond Pattern (using both loop and recursion)

Program to multiply matrices using recursion