Monday, December 22, 2014

C++ Program for solution of Quadratic equation

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
double a , b , c; //quadratic eq coefficients
cout << "Enter the co-efficients of the quadratic eq...\n";
cout << "for x^2: ";
cin >> a;
cout << "for x^1: ";
cin >> b;
cout << "for x^0: ";
cin >> c;
double disc = (b*b) - (4*a*c); //for discriminant

if( disc<0 )
cout << "Discrimnant is Negative! so the solutions will be complexx numbers\n";
else
cout << "THe Discrimnant is +ve so the solution set will consist of real numbers\n";

double X1,X2; //for solutions
X1 = ( -b + sqrt(disc) ) / (2*a);
X2 = ( -b - sqrt(disc) ) / (2*a);
cout << "The solutions are:\n"
<< "{ "
<< X1
<< " , "
<< X2
<< " }"
<< endl;

//for discrimnant
unsigned int choice;
cout << "Press '1' to get the discrimnant of this equation: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "The discrimnant is: "
<< disc
<< endl;
default:
cout << endl;
}

return 0;
}

No comments:

Post a Comment