Quadratic equation solver

Hi

I'm making a program so that it solves quadratic equations (ax^2 + bx + c), bu it doesn't give me any response...

I wrote this:

#include <iostream>
using namespace std;
int main()
{
double a, b, c, r1, r2, delta;
//int a, b, c;
cout << "Introduza os coeficientes (a b c) ";
cin >> a >> b >> c;
delta = pow (b,2) - (4*a*c);
if (delta > 0)
{
r1 = (-b + sqrt(delta))/(2*a);
r2 = (-b - sqrt(delta))/(2*a);
cout << "A equação tem 2 raizes reais: " << r1 << " e " << r2 << endl;
}
if (delta = 0)
{
r1 = (-b)/(2*a);
cout << "A equação tem 1 unica raiz: " << r1 << endl;
}
if (delta < 0)
{
r1 = (-b)/(2*a) + (sqrt (-delta)/(2*a));
r2 = (-b)/(2*a) - (sqrt (-delta)/(2*a));
cout << "A equação tem 2 raizes complexas conjugadas: " << r1 << "i e " << r2 << "i " << endl;
}
return 0;
}

The case in which "delta > 0" is perfectly done, it gives me the right answer, but the other two cases, doesn't give any response at all! I can't find my mistake, can you help me please?
if (delta = 0)
Did you mean
if (delta == 0)?
Hi

Remember Quadratic equation has only real solution if delta >= 0 otherwise it has no real solution, but a complex solution.

As Moschops suggested you have to change your if (delta = 0) into if(delta == 0)

And consider your last case, because what you are doing there is not what the math says


hope it helps
Last edited on
Thank you for your help, it works perfectly now!!
Topic archived. No new replies allowed.