#include <stdio.h>
#include <math.h>
void printQuadraticRoots(float a, float b,float c)
{
float root1, root2, imaginary;
float discriminant;
// Find discriminant of the equation
discriminant = (b * b) - (4 * a * c);
//Check different cases for the discriminant
if(discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
}
else if(discriminant < 0)
{
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
}
}
int main()
{
float a, b, c;
printf("Enter value of 'a' of quadratic equation (aX^2 + bX + c): ");
scanf("%f", &a);
printf("Enter value of 'b' of quadratic equation (aX^2 + bX + c): ");
scanf("%f",&b);
printf("Enter values of 'c' of quadratic equation (aX^2 + bX + c): ");
scanf("%f",&c);
printQuadraticRoots(a,b,c);
return 0;
}
Outout:
Enter value of ‘a’ of quadratic equation (aX^2 + bX + c): 5 Enter value of ‘b’ of quadratic equation (aX^2 + bX + c): 2 Enter values of ‘c’ of quadratic equation (aX^2 + bX + c): 2 Two distinct complex roots exists: -0.20 + i0.60 and -0.20 – i0.60
Outout:
Enter value of ‘a’ of quadratic equation (aX^2 + bX + c): 5
need an explanation for this answer? contact us directly to get an explanation for this answerEnter value of ‘b’ of quadratic equation (aX^2 + bX + c): 2
Enter values of ‘c’ of quadratic equation (aX^2 + bX + c): 2
Two distinct complex roots exists: -0.20 + i0.60 and -0.20 – i0.60