This is the code I tried to get real and complex value for this equation, ax^2 + bx + c = 0.
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, d, x1, x2, i = sqrt(-1);
scanf("%lf %lf %lf", &a, &b, &c);
d = (b * b) - (4 * a * c);
x1 = (-b + sqrt(d)) / (a + a);
x2 = (-b - sqrt(d)) / (a + a);
if (d < 0) {
printf("Complex solution\n");
printf("R1 = %.2lf\n", x1);
printf("R2 = %.2lf\n", x2);
} else
if (a == 0)
printf("Value of a must be non zero\n");
else {
printf("R1 = %.2lf\n", x1);
printf("R2 = %.2lf\n", x2);
}
return 0;
}
And I want to get an answer with i
(eg: -.4 + 4i
).