Create an program that provides the solutions to a quadratic equation. Your prog
ID: 3561557 • Letter: C
Question
Create an program that provides the solutions to a quadratic equation. Your program should be capable of providing real and complex solutions.
A quadratic equation can be generalized by:
Closed form solutions can be found for the zeros of a quadratic function conveniently. That is the locations x where the function is equal to zero can be found by the equation:
Your program should be able to produce real or complex solutions.
Your program should prompt the user to obtain the three values a, b, and c that are needed to define a quadratic equation. After this step, your program should produce the two correct solutions.
For example:
:
Enter the three coefficients of a quadratic function:
1 2 1
First solution: -1.0
Second solution: -1.0
Another example:
:
Enter the three coefficients of a quadratic function:
1 1 1
First solution: -0.5 +i 0.8660254037844386
Second solution: -0.5 -i 0.8660254037844386
Explanation / Answer
/* Program to find roots of a quadratic equation when coefficients are entered by user. */
/* Library function sqrt() computes the square root. */
#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2;
double real, imag;
printf("Enter the three coefficients of a quadratic function: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("First solution: %.2f ",r1);
printf("Second solution: %.2f ", r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("First solution: %.2f ",r1);
printf("Second solution: %.2f ", r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("First solution: %.2f+%.18lfi ",real, imag);
printf("Second solution: %.2f-%.18lfi ", real, imag);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.