C++ program to find roots of quadratic equation ax2 + bx + c=0 (with algorithm)

Here is the C++ code to find roots of quadratic equation ax2  + bx + c=0 . It takes input for a, b and c and find the roots. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked to enter values for a, b and c.
2: Screen displays disc.
3: If discernment is greater than zero Roots are real and unequal
4: If discernment is less than zero Roots are imaginary 
5: If discernment is equal to zero Roots are equal
Developer side
1: Declare variables that are going to be used in program .
2: Input variables a,b and c
3: Place an if statement to check if all are non-zero
4:  Place an if statement to check if all are positive
5: If above two conditions are true then calculate discernment using following statement disc=(b*b)-4*a*c;
6: If discernment is greater than zero Roots are real and unequal and calculated using following formulas root1=(-b+sqrtf(disc) /(2*a));
         root2=(-b-sqrtf(disc) /(2*a)); 
7: If discernment is less than zero Roots are imaginary and calculated using following formulas 
root1=sqrt(disc)<<" /"<<2*a;
root2=sqrt(disc)<<" /"<<2*a
8: If discernment is equal to zero Roots are equal and calculated using following formulas 
root1=-b/(2*a);
root2=root1;
9: Program ends here.
Here is the C++ code.

Code:

#include<iostream>
#include<math.h>
using namespace std;
void main()
{      float a,b,c;
       float disc,root1,root2;
       int i=-1;
       cout<<"Find roots of quadratic equation ax2  + bx + c=0 "<<endl<<"where a b and c are scalers"<<endl;
       cout<<"Enter value of a"<<endl;
       cin>>a;
       cout<<"Enter value of b"<<endl;
       cin>>b;
       cout<<"Enter value of c"<<endl;
       cin>>c;
       if(a==0||b==0||c==0)
              {
                     cout<<"The value cannot be zero"<<endl;
              }
       else if(a<0||b<0||c<0)
                     {
                           cout<<"Values must be greater than Zero"<<endl;
                     }
              else { disc=(b*b)-4*a*c;
                           //disc>0
                           if(disc>0)
                                  {      cout<<"disc>0 so"<<endl;
                                         cout<<"Roots are real and unequal"<<endl;
                                         root1=(-b+sqrtf(disc) /(2*a));
                                         root2=(-b-sqrtf(disc) /(2*a));
                                         cout<<"First root is = "<<root1<<endl;
                                         cout<<"Second root is = "<<root2<<endl;
                                  }
                           else //disc<0
                                  if(disc<0)
                                         {      disc=(-1)*disc;
                                                cout<<"disc<0 so"<<endl;
                                                cout<<"Roots are imaginary "<<endl;
                                                cout<<"First root=(-"<<b<<"+i*"<<sqrt(disc)<<" /"<<2*a<<")"<<endl;
                                                cout<<"Second root=(-"<<b<<"-i*"<<sqrt(disc)<<" /"<<2*a<<")"<<endl;
                                               
                                           
                                               

                                         }
                                  else { //disc=0
                                                cout<<"disc=0 so"<<endl;
                                                cout<<"Roots are equal"<<endl;
                                                root1=-b/(2*a);
                                                root2=root1;
                                                cout<<"First root is = "<<root1<<endl;
                                                cout<<"Second root is = "<<root2<<endl;
                                          }
                          
                      }
       system("pause");
}

Output:


Comments