C++ Arithmetic Operator Solves for the roots of a quadratic equation

Description

C++ Arithmetic Operator Solves for the roots of a quadratic equation

#include <iostream>
#include <cmath>
using namespace std;
int main()/*from w  ww . j  ava  2 s. c  o  m*/
{
   double a, b, c, disc, root1, root2;
   cout << "calculates the roots of a quadratic equation of the form:ax^2 + bx + c = 0\n\n";
   cout << "Please enter values for a, b, and c: ";
   cin  >>  a >> b >> c;
   if (a == 0.0 && b == 0.0)
      cout << "The equation is degenerate and has no roots.\n";
   else if (a == 0.0)
      cout << "The equation has the single root x = " << -c/b << endl;
   else
   {
      disc = pow(b,2.0) - 4 * a * c;    // calculate discriminant
      if (disc > 0.0)
      {
         disc = sqrt(disc);
         root1 = (-b + disc) / (2 * a);
         root2 = (-b - disc) / (2 * a);
         cout << "The two real roots are " << root1 << " and " << root2 << endl;
      }
      else if (disc < 0.0)
         cout << "Both roots are imaginary.\n";
      else
         cout << "Both roots are equal to " << -b / (2 * a) << endl;
      }
      return 0;
}



PreviousNext

Related