Write a C++ programming code for a non-zero, discuss the roots of the quadratic equation as follows:
a) D =0; the equation has a double solution.
x1 = x2 = -b/2a
b) D>0; the question has two different solutions x1 and x2.
x1= (-b-sqrt(D))/2a
x2= (-b+sqrt(D))/2a
c) if D<0; the equation has no real solution
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
double x1, x2;
double determinant;
cout << "Enter coefficients a, b and c: " <<endl;
cin >> a >> b >> c;
determinant = b*b - 4*a*c;
if (determinant == 0)
{
cout << "The equation has a double solution." <<
endl;
x1 = (-b ) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else if (determinant > 0) {
x1 = (-b + sqrt(determinant)) / (2*a);
x2 = (-b - sqrt(determinant)) / (2*a);
cout << " The equation has two different solutions.Roots are
real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else {
cout << "The equation has no real solution" <<
endl;
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.