// File: quadratic.cxx // Michael Main (main@colorado.edu) // THis program prints the two real roots of a quadratic // equation. It illustrates an "if ... else" statement. #include // For cin, cout #include // For sqrt() using namespace std; int main( ) { double a, b, c; // Coefficients of a quad. equation double discriminant; // Value of b*b - 4ac double root; // One root of the quadratic equation // Read the input. cout << "Please type the abc coefficients of a quadratic\n"; cout << "equation of the form a*x*x + b*x + c = 0.\n"; cout << "Enter a b c: "; cin >> a >> b >> c; // Calculations, using a repeated if-else discriminant = b*b - 4*a*c; if (a == 0) { cout << "This program can't handle a == 0.\n"; } else if (discriminant < 0) { cout << "That equation has no real roots.\n"; } else { root = (-1.0*b + sqrt(discriminant)) / (2*a); cout << "First root is: " << root << endl; root = (-1.0*b - sqrt(discriminant)) / (2*a); cout << "Second root is: " << root << endl; } return 0; }