Thursday, December 18, 2014

To find the nature and value of roots of quadratic equation except for complex roots

/*c++ Program to find the nature of roots and value of
roots of a quadratic equation.User has to enter the values of coefficients
of x^2,x and the value of constant*/


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  float a,b,constt,discre,root1,root2,k;  //declaring variables
  do
  {
  cout<<"Enter coeffitient value of x^2 = ";
     cin>>a;         //input the value of a the coeffitient of x^2
  }while(a==0);        //checking condition
  cout<<"Enter coeffitient value of x = ";  //asking to give input (value of coeffitient of x)
  cin>>b;          //input the (value of coeffitient of x)
  cout<<"Enter value of constant = ";    //asking to give value of constant
  cin>>constt;          //input the value of constant
  discre=pow(b,2)-4*a*constt;      //k is used as to store a temperarly intermediate result
  k=sqrt(discre);
   if(discre==0)
   {
     root1=(-b+k)/(2*a);  
     cout<<"The equation has a single(repeated) root\n\n";
     cout<<"first root = "<<root1<<endl;
   }
  else if(discre>0)
  {  
   root1=(-b+k)/(2*a);
   root2=(-b-k)/(2*a);
   cout<<"Equation has two real roots.\n\n";
      cout<<"First real root = "<<root1<<endl;
      cout<<"Second real root = "<<root2<<endl;
     }
  else
  {
    cout<<"The equation has two complex roots\n\n";
  }
 return 0;
}

No comments:

Post a Comment