Blog
C++ Program to Find Quadratic Equation Roots
Overview
C++ Program to Find Quadratic Equation Roots.
-b + SQRT( b^2 – 4ac)
Root 1 = ——————————-
2a
-b – SQRT( b^2 – 4ac)
Root 2 = ——————————-
2a

C++ Program
#include
#include
using namespace std;
// ########################################################
//
// -b + SQRT( b^2 - 4ac)
// Root 1 = -------------------------------
// 2a
//
// -b - SQRT( b^2 - 4ac)
// Root 2 = -------------------------------
// 2a
//
//
// www.TestingDocs.com
//##########################################################
int main()
{
float a = 3.0, b = -12.0, c = -36.0;
// Equation is 3x^2 -12x - 36 = 0
float sqRootPart = sqrt( b*b - 4*a*c );
//Find the roots
float eqRoot1 = (-b + sqRootPart)/(2*a);
float eqRoot2 = (-b - sqRootPart)/(2*a);
//Output
cout << "Root 1 := " << eqRoot1 << endl;
cout << "Root 2 := " << eqRoot2 << endl;
return 0;
}

Sample Output
Enhancements
We can further enhance the program to prompt the input values of a, b, c to the user. Right now we have to modify the code for the values.
Generate the equation string based on the values of a, b, and c and displayĆ the equation string in the output.
Take into consideration the imaginary roots if discriminant is less than 0.
For more information on Code Blocks IDE, visit the official website of Code blocks IDE:
http://www.codeblocks.org/