Roots of a quadratic equation


#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); float a,b,c,d,e,f; cout<<"Enter the coefficients a,b,c of X in a quadratic equation:"; cin>>a>>b>>c; d=sqrt((b*b)-(4*a*c)); if(d>0) { e=((-b+d)/2*a); f=((-b-d)/2*a); cout<<"First square root is:"<<e<<endl; cout<<"Second square root is:"<<f<<endl; } else { cout<<"The two roots are not real"<<endl; } getch(); }
*********output Section***********
Enter the coefficients a,b,c of X in a quadratic equation
:1
    
-1                                                         
-2                                        
                                     
First root is:2                                                                
Second root is:-1                                                           
Algorithm:
  1. Read A,B,C
  2. Set D = B2- 4*A*C                 
  3. If D > 0, then :
a)      Set E = - B + sqrt (D)/2*A
And F= - B - sqrt (D)/2*A
b)     Write: E, F
       Else
               Write: ‘NO REAL SOLUTION ‘
       [End of If structure]
       4. Exit

0 comments:

Post a Comment