xxxxxxxxxx
ax2 + bx + c = 0, where
a, b and c are real numbers and
a != 0
x = \frac{-b\pm \sqrt{b^2-4ac}}{2a}
xxxxxxxxxx
#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, determinant, r1, r2, real, imag;
scanf("%f%f%f", &a, &b, &c);
determinant == b*b - 4*a*c;
if(determinant > 0) // both roots are real
{
r1 = (-b + sqrt(determinant))/2*a; // Brackets are important
r2 = (-b - sqrt(determinant))/2*a;
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
else if(determinant == 0) // both roots are real and equal
{
r1 = r2 = -b/(2*a); // brackets are important
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
/*
Determinant < 0 - both roots are imaginary of the
form real + i*imaginary
*/
else
{
real = -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
}
}