Program 8) An equation of the form
ax(square) + 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:
root 1 = -b + sqrt(b(square) - 4ac)/ 2a
root 2 = -b - sqrt(b(square) - 4ac)/ 2a
A program to evaluate these roots. The program requests the user to input the values of a, b and c and outputs root 1 and root 2.
PROGRAM
#incluce <math.h>
main( )
{
float a, b, c, discriminant, root1, root2 ;
printf("Input values of a, b, and c\n") ;
scanf("%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) ;
}
getch( ) ;
}
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
The term (b(square) - 4ac) is called the discriminant. If the discriminant is less than zero, its square rots cannot be evaluated. In such cases, the roots are said to be imaginary numbers and the program outputs an appropriate message.
No comments:
Post a Comment