/****************************************************** * * * * * 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, d, com_root; // 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; cout << "The given second degree equation is:" << endl << endl; cout << a << " * x^2 + " << b << " * x + " << c << " = 0" << endl << endl;; d = (pow(b,2) - 4 * a * c); if (d >= 0) { cout << "Real Roots" << endl << endl; root1 = (-b + sqrt(d)) / (2 * a); root2 = (-b - sqrt(d)) / (2 * a); cout << "Root1 = " << root1 << " (unformatted)" << endl << endl; cout << "Root2 = " << root2 << " (unformatted)" << endl << endl << endl; 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; } else { cout << "Complex Roots" << endl << endl; com_root = (sqrt(fabs(d))) / (2 * a); cout << "Root1 = " << -b / (2 * a) << " + i * " << com_root << " (unformatted)" << endl << endl; cout << "Root2 = " << -b / (2 * a) << " - i * " << com_root << " (unformatted)" << endl << endl; cout << "Root1 = " << -b / (2 * a) << " + i * " << setprecision(2) << fixed << com_root << " (formatted to two decimal places)" << endl << endl; cout << "Root2 = " << -b / (2 * a) << " - i * " << setprecision(2) << fixed << com_root << " (formatted to two decimal places)" << endl << endl; } return 0; }//end function main