Roots Of Quadratic Equation


Program to calculate the roots of quatratic equation in C++





#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
          clrscr();
          int a,b,c,mt;
          float x1,x2;
          cout<<"\nEnter Coefficient of x^2 :: ";
          cin>>a;
          cout<<"\nEnter Coefficient of x :: ";
          cin>>b;
          cout<<"\nEnter Constant :: ";
          cin>>c;
          mt=b*b-4*a*c;
          if(mt>=0)
          {
                    x1=(-b+sqrt(mt))/(2*a);
                    x2=(-b-sqrt(mt))/(2*a);
                    cout<<"\nRoots of given quatratic equation are "<<x1<<" and "<<x2;
          }
          else
          {
                    cout<<"\nRoots are imaginary and complex.";
          }
          getch();
}


OUTPUT::

Enter Coefficient of x^2 :: 3

Enter Coefficient of x :: 5

Enter Constant :: 6

Roots are imaginary and complex.



-----

Firoz Memon

Please view my other blogs:

          C++ Codes 4 Beginners

          Java Tips

          Java 4 Beginners

Previous Post Next Post