The below program ask the user to enter the value of a,b and c. After getting the value from the user it will calculate on the basis of ‘Discriminant’ value.
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
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);
// 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);
}
return 0;
}
Output:
Enter value of a of quadratic equation (aX^2 + bX + c): 2 Enter value of b of quadratic equation (aX^2 + bX + c): 7 Enter values of c of quadratic equation (aX^2 + bX + c): 2 Two distinct and real roots exist: -0.31 and -3.19
The below program ask the user to enter the value of a,b and c. After getting the value from the user it will calculate on the basis of ‘Discriminant’ value.
Output:
Enter value of a of quadratic equation (aX^2 + bX + c): 2
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): 7
Enter values of c of quadratic equation (aX^2 + bX + c): 2
Two distinct and real roots exist: -0.31 and -3.19