home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / if5.cpp < prev    next >
C/C++ Source or Header  |  1993-03-19  |  1KB  |  47 lines

  1. // C++ program to solve quadratic equation
  2.  
  3. #include <iostream.h>
  4. #include <math.h>
  5.  
  6. main()
  7. {          
  8.   double A, B, C, discrim, root1, root2, twoA;
  9.  
  10.   cout << "Enter coefficients for equation A*X^2 + B*X + C\n";  
  11.   cout << "Enter A: ";
  12.   cin >> A;
  13.   cout << "Enter B: ";
  14.   cin >> B;
  15.   cout << "Enter C: ";
  16.   cin >> C;
  17.   
  18.   if (A != 0) {
  19.      twoA = 2 * A;
  20.      discrim = B * B - 4 * A * C;
  21.      if (discrim > 0) {
  22.        root1 = (-B + sqrt(discrim)) / twoA;
  23.        root2 = (-B - sqrt(discrim)) / twoA;
  24.        cout << "root1 = " << root1 << "\n";
  25.        cout << "root2 = " << root2 << "\n";
  26.      }
  27.      else if (discrim < 0) {                 
  28.  
  29.        discrim = -discrim;
  30.        cout << "root1 = (" << -B/twoA 
  31.             << ") + i (" << sqrt(discrim) / twoA << ")\n";
  32.        cout << "root2 = (" << -B/twoA 
  33.             << ") - i (" << sqrt(discrim) / twoA << ")\n";
  34.      }
  35.      else {
  36.        root1 = -B / 2 / A;
  37.        root2 = root1;
  38.        cout << "root1 = " << root1 << "\n";
  39.        cout << "root2 = " << root2 << "\n";       
  40.      }
  41.   }
  42.   else
  43.     cout << "root = " << (-C / B) << "\n";
  44.   
  45.   
  46.   return 0;
  47. }