Advertisement
TheBeastOver9000

Quadratic equation solver

Apr 28th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <locale>
  7.  
  8. //Quadratic equation solver
  9.  
  10. using namespace std;
  11.  
  12. int D, a, b, c, x1, x2;
  13. double qq;
  14.  
  15. int main(){
  16.     cout << "Sqrt is a sqare root\n";
  17.     cout << "Enter a\n";
  18.     cin >> a;
  19.     cout << "Enter b\n";
  20.     cin >> b;
  21.     cout << "Enter c\n";
  22.     cin >> c;
  23.     D = (pow(b, 2) - (4 * a * c));
  24.     if (D < 0){
  25.         cout << "X1, X2 = varnothing\n";
  26.         system("pause");
  27.         return 0;
  28.     }
  29.     else if (D == 0){
  30.         x1 = (b * -1) / (2 * a);
  31.         x2 = x1;
  32.     }
  33.     else{
  34.         qq = sqrt(D);
  35.         if (D / qq == qq){
  36.             x1 = ((b * -1) - qq) / (2 * a);
  37.             x2 = ((b * -1) + qq) / (2 * a);
  38.         }
  39.         else{
  40.             a = a * 2;
  41.             cout << "X1 = -" << b << " - sqrt of" << D << "/ " << a << "\n";
  42.             cout << "X2 = -" << b << " + sqrt of" << D << "/ " << a << "\n";
  43.             system("pause");
  44.             return 0;
  45.         }
  46.     }
  47.    
  48.     cout << "X1 = " << x1 << "\nX2 = " << x2 << "\n";
  49.     system("pause");
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement