C++ Program To Solve Quadreatic Equation

C++ Program To Solve Quadreatic Equation

//C++ Program To Solve Quadreatic Equation
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    float a,b,c;
  float d,root1,root2;


  cout<<"Enter a, b and c of quadratic equation ax^2+bx+c:--\n";
  cout<<"Value of a:--";
  cin>>a;
  cout<<"Value of b:--";
  cin>>b;
  cout<<"Value of c:--";
  cin>>c;

  d = b * b - 4 * a * c;

  if(d < 0){
    cout<<"Roots are complex number.\n";
    cout<<"Roots of quadratic equation are: ";
    cout<<"\nRoot 1:--"<<-b/(2*a)<<"+"<<sqrt(-d)/(2*a)<<"i";
    cout<<"\nRoot 2:--"<<-b/(2*a)<<-sqrt(-d)/(2*a)<<"i";
  }
  else
  if(d==0)
  {
   cout<<"Both roots are equal.\n";
   root1 = -b /(2* a);
   cout<<"Root of quadratic equation is:--"<<root1;
  }
  else
  {
   cout<<"Roots are real numbers.\n";
   root1 = ( -b + sqrt(d)) / (2* a);
   root2 = ( -b - sqrt(d)) / (2* a);
   cout<<"Roots of quadratic equation are:--"<<root1<<","<<root2;
  }

}

Post a Comment

0 Comments