/****************************************************** * * * * * CSCI 1470, Fall 2005 * * Machine Problem: MP3 * * Purpose: Display the roots of the quadratic * * equation. * ******************************************************/ #include #include #include using namespace std; int main(void) { //Declare variables double a, b, c, root1, root2; // Get data from user with prompt cout << "Enter the coefficient of x^2 in the equation:"; cin >> a; cout << endl; cout << "Enter the coeffecient of x in the equation:"; cin >> b; cout << endl; cout << "Enter the constant in the equation:"; cin >> c; cout << endl; //Display the equation with user entered data cout << "The given second degree equation is:" << endl << endl; cout << a << " * x^2 + " << b << " * x + " << c << " = 0" << endl << endl;; //Calculate the roots with user entered data root1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); root2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); //Display the roots unformatted cout << "Root1 = " << root1 << " (unformatted)" << endl << endl; cout << "Root2 = " << root2 << " (unformatted)" << endl << endl << endl; //Display the roots formatted to two decimal places cout << "Root1 = " << setprecision(2) << fixed << root1 << " (formatted to two decimal places)" << endl << endl; cout << "Root2 = " << setprecision(2) << fixed << root2 << " (formatted to two decimal places)" << endl << endl; return 0; }//end function main