Find the roots of quadratic equation in C++

In this program, we will find the roots of quadratic equation by handling all the possible cases in C++.
Submitted by Indrajeet Das, on November 08, 2018

The "roots" of the quadratic are the numbers that satisfy the quadratic equation. There are always two roots for any quadratic equation, although sometimes they may coincide.

The roots can be real or imaginary.

The roots of any quadratic equation is given by: x = [-b +/- sqrt(-b^2 - 4ac)]/2a.

Algorithm:

1. Create a class roots with coefficients a, b and c and roots r1 and r2 as data members and getdata(), determinant() and checkdeterminant() as member functions.
2. getdata() takes input from user and initialize the data members a, b, c and define determinant() so that it can compute (b*b)-(4*a*c). If the determinant is 0 then the roots must be equal so assign them the value –b/2a and print the result. If the determinant is positive then roots must be distinct and real so calculate two roots -b+sqrt(d)/2a and -b-sqrt(d)/2a and print the result. If the determinant is negative then roots must be imaginary so calculate real and virtual part –b/2a and sqrt(-d)/2a and print result.
3. Create an object r of class roots.
4. Input the coefficients using the getdata().
5. Compute determinant using determinant().
6. Check determinant and print the roots using checkdeterminant().

C++ code to find the roots of quadratic equation

```#include <iostream>
#include <cmath>
using namespace std;

//class
class roots {
int a, b, c;
float r1, r2;
public:
void getdata();
int determinant();
void checkdeterminant(int);
};

void roots::getdata() {
cout << "Enter value of coefficient of x^2: ";
cin >> a;
cout << "Enter value of coefficient of x: ";
cin >> b;
cout << "Enter value of coefficient of 1: ";
cin >> c;
}

int roots::determinant() {
int d = b * b;
d -= (4 * a * c);
return d;
}

void roots::checkdeterminant(int d) {
if (d == 0) {
cout << "Real and equal roots\n";
r1 = (-1 * b);
r1 /= (2 * a);
r2 = r1;
cout << "Roots : " << r1 << " and " << r2 << endl;
}
else if (d > 0) {
cout << "Real and distinct roots\n";
r1 = (-1 * b) + sqrt(d);
r1 /= (2 * a);
r2 = (-1 * b) - sqrt(d);
r2 /= (2 * a);
cout << "Roots : " << r1 << " and " << r2 << endl;
}
else {
cout << "Imaginary roots" << endl << endl;
r1 = (-b)/(2*a);
r2 = (sqrt(-d))/(2*a);
cout << "Roots : " << r1 << " + i" << r2 ;
cout << " and " << r1 << " - i" << r2 << endl<<endl;
}
}

int main() {
roots r;

r.getdata();

cout << endl;
int d = r.determinant();
r.checkdeterminant(d);
cout << endl;

return 0;
}
```

Output

```First run:
Enter value of coefficient of x^2: 1
Enter value of coefficient of x: 4
Enter value of coefficient of 1: 4

Real and equal roots
Roots : -2 and -2

Second run:
Enter value of coefficient of x^2: 1
Enter value of coefficient of x: -5
Enter value of coefficient of 1: 6

Real and distinct roots
Roots : 3 and 2

Third run:
Enter value of coefficient of x^2: 1
Enter value of coefficient of x: 3
Enter value of coefficient of 1: 4

Imaginary roots
Roots : -1 + i1.65831 and -1 - i1.65831
```

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.