Thursday, December 14, 2017

C++ Quadratic Equation

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double a, b, c, discriminant, x1, x2, imaginary;
    cin >> a >> b >> c;
discriminant = b*b - 4*a*c;

if (a == 0)
 {
cout << "Can't divide by 0!" << endl;
}

        else if (discriminant > 0)
        {
            x1 = (-b + sqrt(discriminant)) / (2*a);
            x2 = (-b - sqrt(discriminant)) / (2*a);

            cout << "x1 = " << x1 << endl;
            cout << "x2 = " << x2 << endl;
        }

    else if (discriminant == 0)
    {
        x1 = (-b + sqrt(discriminant)) / (2*a);
        cout << "x1 = x2 =  " << x1 << endl;
    }

        else if (imaginary = -b/(2*a))
        {
            cout << "Values are imaginary!"  << endl;
        }

return 0;

}

No comments:

Post a Comment