Roots of a quadratic equation in C

18CPL17/27

Program 2:-

Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.

Quadratic Equation?

ax2 + bx + c = 0
a=0 not a quadratic equation.
Roots of quadratic equation x=(​​-b+ or -sqrt(b*b-4*a*c))/2*a
(b*b − 4ac) is Determinant(D).
If D=0 (root are equal)
If D>0(roots are distinct and real)
If D<0(roots are imaginary) and have two-part
real=-b/(2*a);
imaginary=sqrt(fabs(determinant))/(2*a);

ALGORITHM 

 Purpose: To find roots of a given quadratic equation.

 Input: Coefficients of the quadratic equation a, b, c

 Output: Root1 and Root2 

 START 

 STEP 1: [Input the values of a, b, c] 
 read a, b, c 

 STEP 2: [Calculate the determinant] 
 determinant = b*b-4*a*c 

 STEP 3: [Check for validity] 
 If a is equal to 0 and b is equal to 0 
 print “Invalid Inputs” 

 STEP 4: [Check for different roots] 
 If a is equal to 0 
 print “Linear equation” 
 Root1=-c/b 
 Print “Root1” 

 STEP 5: [Check for real and equal roots] 
 If determinant is equal to 0 
 print “Roots are real and equal” 
 Root1= -b/(2*a) 
 Root2 = -b/(2*a) 
 Print “Root1 & Root2” 

 STEP 6: [Check for real and distinct roots] 
 If determinant is greater than 0 
 Then print “Roots are real and distinct” 
 Root1= (-b+ (sqrt (fabs (determinant))))/(2*a) 

 Root2= (-b-(sqrt (fabs (determinant))))/(2*a)
Print “root1 and root2” 
 End if 

 STEP 7: [Check for imaginary roots] 
 print “Roots are imaginary” 
 Real=-b/ (2*a) 
 Imaginary=sqrt (fabs (determinant))/ (2*a) 
 print “Root1 and R oot2” 

 STEP 8: [Finished] 

 STOP 

FlowChart:-


C Code:-



 ****************************************************************************** 
 Output 1: 
 Enter the Co-efficient of Quadratic Equation 
 000 
 Invalid Coefficients 

Output 2: 
 Enter the Co-efficient of Quadratic Equation 
 0 21 Root1=-0.5

 Output 3: 
 Enter the Co-efficient of Quadratic Equation 
 121 
 Roots are Real and Equal 
 Root1=-1.0000 
 Root2=-1.0000

Output 4: 
 Enter the Co-efficient of Quadratic Equation 
 189 
 Roots are Real and Distinct 
 Root1=-1.354 
 Root2=-6.646 

Output 5: 
 Enter the Co-efficient of Quadratic Equation 
 123 
 ROOTS ARE IMAGINARY 
 ROOT1=-1.000+i1.414 
 ROOT2=-1.000-i1.414 
 ****************************************************************************** 

No comments:

For Query and doubts!

Powered by Blogger.