An equation of
the form
ax2 + bx + c = 0
is known as the quadratic
equation. The values of x that
satisfy the equation are known as the roots
of the equation. A quadratic equation
has two roots which are given by the following two formulae:
-b + sqrt(b2 - 4ac)

2a
-b - sqrt(b2 - 4ac)

2a
A program to evaluate these roots is given in Fig.3.10. The program
requests the user to input the values of a,
b and c and outputs root1 and root2.
Program
#include <math.h>
main()
{
float a, b, c, discriminant,
root1, root2;
printf("Input values of a, b, and c\n");
scanf("%f %f %f", &a, &b, &c);
discriminant = b*b - 4*a*c ;
if(discriminant < 0)
printf("\n\nROOTS ARE IMAGINARY\n");
else
{
root1 = (-b + sqrt(discriminant))/(2.0*a);
root2 = (-b - sqrt(discriminant))/(2.0*a);
printf("\n\nRoot1 = %5.2f\n\nRoot2 = %5.2f\n",
root1,root2 );
}
}
Output
Input
values of a, b, and c
2 4
-16
Root1
= 2.00
Root2 =
-4.00
Input
values of a, b, and c
1 2
3
ROOTS ARE IMAGINARY
No comments:
Post a Comment