Search This Blog

Tuesday 2 February 2016

Roots of Quadratic equation using Classes

#include<iostream>
#include<math.h>

using namespace std;

class quadratic
{
int a,b,c,d;
float x1,x2;
public:
quadratic();
void roots();
};

quadratic::quadratic()
{
cout << "Enter value of x^2" << endl;
cin >> a;
cout << "Enter value of x^1" << endl;
cin >> b;
cout << "Enter value of x^0" << endl;
cin >> c;
}

void quadratic::roots()
{
d=(pow(b,2)-4*a*c);
if(d<0)
cout << "Roots are imaginary" << endl;
else if(d==0)
{
x1=x2=-b/(2*a);
cout << "Roots are real and equal for x = " << x1 << endl;
}
else
{
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
cout << "Roots are real and distinct and x1 = "<< x1 << " and x2 = " << x2 << endl;
}
}
int main()
{
quadratic a;
a.roots();
return 0;
}

No comments:

Post a Comment