This program will solve quadratic equations. It accepts coefficients of a quadratic equation from the user i.e. a, b and c and displays the roots.

To compile the program name it quadratic_solver.cpp then type g++ -o quadratic_solver quadratic_solver.cpp

You may need to use math.h like this: #include if you are using windows C++ programming software. (I tried it without the math.h and got an “undeclared identifier” error)

What is Quadratic Equation?

The Quadratic equation is the equation of the form as below:

ax2 + bx +c = 0

Where x represents unknown and a, b and c are coefficients, it’s roots is given by following the formula.

Quadratic Formula

Quadratic Formula

Here,

The term b2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots.

  1. If discriminant is greater than 0, the roots are real and different.
  2. If discriminant is equal to 0, the roots are real and equal.
  3. If discriminant is less than 0, the roots are complex and different.

C++ Program to Solve Quadratic Equation

 

Output of C++ Program

Compile: $ g++ -o quadratic_solver quadratic_solver.cpp

Run:

$ ./quadratic_solver
Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:
Enter value for a:
6
Enter value for b:
4
Enter value for c:
1
The roots are not real numbers
x1 =-0.333333 + 0.235702 * i
x2 =-0.333333 + -0.235702 * i
Press c and then Enter to continue….
c
Done

$ ./quadratic_solver
Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:
Enter value for a:
9
Enter value for b:
24
Enter value for c:
2
The roots are:
x1 = -0.0861142 , x2 = -2.58055
Press c and then Enter to continue….
c
Done

1 Shares