You are on page 1of 2

// First Lab project

// Quadratic Equation Solver


// [-b + or - (b^2 -4ac)^(1/2)] / 2a
#include <iostream>
#include <cmath>
using namespace std;
int main(){
float varA; //User declares variable a
float varB; //User declares variable b
float varC; //User declares variable c
float X1; //Solution using addition
float X2; //Solution using subtraction
float X3; //Special case a=0
float disc; //Discriminant of formula
cout << "Quadratic Equation Solver"<< endl;
cout << "Enter value of variable a: "<< endl;
cin >> varA;
cout << "Enter value of variable b: "<< endl;
cin >> varB;
cout << "Enter value of variable c: "<< endl;
cin >> varC;
cout << showpos << "Polynomial is set to: " << varA << "x^2 " << varB << "x
" << varC << endl;
cout << showpos << "The monic form of this equation is: x^2 " << varB / varA
<< "x " << varC / varA << endl;
disc = (pow(varB,2) - 4 * varA * varC);
cout << showpos << "The discriminant of the equation is: " << disc << endl;
if(varA == 0 && varB == 0 && varC == 0){
cout << "X is any real value, because the equation becomes 0 = 0"<< endl
;
}
else if (varA == 0 && varB == 0 && varC != 0){
cout << "No possible solutions, because the equation becomes c = 0"<< en
dl;
}
else if (varA == 0 && varC == 0 && varB != 0){
cout << "Solution is equal to the y-axis, because the equation becomes x
= 0"<< endl;
}
else if (varA == 0){
X3 = -varC / varB;
cout << "The solution is the vertical line that crosses the x-axis at th
e point " << X3 << ".";
}
else if (disc < 0){
cout << "No real solutions, because the discriminant is a negative numbe
r";
}
else if (disc == 0){
X1 = (-1 * varB + sqrt(disc)) / (2 * varA);

cout << "since it is 0, there is only one solution, which is: " << X1 <<
endl;
}
else {
cout << "Because the discriminant is
<< endl;
X1 = (-1 * varB + sqrt(disc)) / (2 *
if(X1 > 0)
cout << "The first solution is "
else {
cout << "The first solution is "
}

positive, there are two solutions."


varA);
<< X1 << "."<< endl;
<< X1 << "."<< endl;

X2 = (-1 * varB - sqrt(disc)) / (2 * varA);


if (X2 > 0)
cout << "The second solution is " << X2 << ".";
else {
cout << "The second solution is " << X2 << ".";
}
}
return 0;
}

You might also like