Description: Create a C program that finds the roots/solutions to a quadratic eq
ID: 3902130 • Letter: D
Question
Description: Create a C program that finds the roots/solutions to a quadratic equation. Read in the coefficients a, b, and c of a quadratic and display the roots (or root) of the equation. Notes: ----- - Read in the coefficients as integers - Compute the root(s) as floating-point values - Nicely format and label your output. - Avoid the complex datatype. If you have imaginary/ complex solutions, compute the real and imaginary parts as real/floating-point numbers, and then display the real and imaginary parts together as a complex number - Also display imaginary/complex solutions in a reasonable format. Ex: root1 = -1.00 + 1.00i root2 = -1.00 - 1.00i
Explanation / Answer
Below is your program
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c;
double determinant, root1,root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%d %d %d",&a, &b, &c);
determinant = b*b-4*(double)a*c;
// condition for real and different roots
if (determinant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(determinant))/(2*(double)a);
root2 = (-b-sqrt(determinant))/(2*(double)a);
printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
}
//condition for real and equal roots
else if (determinant == 0)
{
root1 = root2 = -(double)b/(2*(double)a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else
{
realPart = -(double)b/(2*(double)a);
imaginaryPart = sqrt(-determinant)/(2*(double)a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
Output
Enter coefficients a, b and c: 2 4 5
root1 = -1.00+1.22i and root2 = -1.00-1.22i