C++ quadratic equation using factorization

Building on what @Aditi Rawat said, I’ve wrote a little program that does what you need using the quadratic roots formula. Not sure if you need to do it that way or not but this solves the problem:

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

int main()
{
    double a, b, c;
    cout<<"Enter a: "; cin>>a; cout<<endl;
    cout<<"Enter b: "; cin>>b; cout<<endl;
    cout<<"Enter c: "; cin>>c; cout<<endl;

    double x_positive, x_negative;
    if((pow(b,2) - 4*a*c) >= 0) {
      x_positive = (-b + sqrt(pow(b,2) - 4*a*c)) / 2*a;
      x_negative = (-b - sqrt(pow(b,2) - 4*a*c)) / 2*a;
      cout << "Root 1: " << x_positive << endl;
      cout << "Root 2: " << x_negative << endl;
    } else cout << "Roots are complex" << endl;

    return 0;
}

Browse More Popular Posts

Leave a Comment