Guest User

Untitled

a guest
Sep 18th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. //Rodrigo Santillan
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9. void discrim(int a, int b, int c);
  10.  
  11. int main()
  12. {
  13.     int a;
  14.     int b;
  15.     int c;
  16.     char re;
  17.  
  18.  
  19.     cout << "This program will calculate the roots of your quadratic. \nEnter your coeffecients of your quadratic.\n";
  20.  
  21.         do
  22.         {
  23.             cin >> a >> b >> c;
  24.  
  25.             cout << "Your quadratic equation is:\n" << a << "x^2";
  26.             if (b > 0)
  27.             {
  28.                 cout << "+" << b << "x";
  29.             }
  30.             else
  31.             {
  32.                 cout << b << "x";
  33.             }
  34.             if (c > 0)
  35.             {
  36.                 cout << "+" << c;
  37.             }
  38.             else
  39.             {
  40.                 cout << c << endl;
  41.             }
  42.  
  43.             cout << "\nWould you like me to calculate some more roots? (y or n)\n";
  44.             cin >> re;
  45.             if ( re == 'y')
  46.             {
  47.                 discrim(a, b, c);
  48.                 break;
  49.             }
  50.             else
  51.             {
  52.                 cout << "Program will now exit\n";
  53.                 break;
  54.             }
  55.         } while (re == 'y');
  56.     return 0;
  57. }
  58.  
  59.  
  60. void discrim(int a, int b, int c)
  61. {
  62.     int z = (pow(b, 2) - 4 * a * c);
  63.     if (z > 0)
  64.     {
  65.         int x1 = (-1 * b + sqrt(z));
  66.         int x2 = (-1 * b - sqrt(z));
  67.         cout << "Your Roots are x = " << x1 << " and x = " << x2 << endl;
  68.     }
  69.     else if (z == 0)
  70.     {
  71.         int xonly = (-1 * b) / (2 * a);
  72.         cout << "Your only root is x = " << xonly;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment