Developing Applications in C++ or rather any language is not difficult at all. All you need is a bit dedication and focus on your project, besides the basic knowledge about your task and the programming language that you are using. In today’s tutorial, I am going to discuss how to develop an application that will solve a quadratic equation for you and for doing this I will use C++. Also, I use Turbo C++ for writing and executing my C++ Codes. If you are running Windows 7 or Windows 8, you might want to read how to run Turbo C++ on your computer. Here is a simple tutorial for that. Also, you might want to use Notepad++ for writing down your code and then executing it on Turbo C++.

**How to Install Turbo C on Windows 7 and Windows 8**-
**Using Notepad++ for enhanced Programming Experience**

So let’s get started with the tutorial. Before we begin, I assume that you are aware with the basics of C++ Programming and the various basic functions in C++ like square, square root and all. Also, I assume that you know how to solve a quadratic equation on paper. Well, I could just give you all the code for the entire application in one go and ask you to simply run it. But, instead I would like to go step by step, explaining as much as I can within each step. Let’s go.

### Quadratic Equation Solver in C++

If you are unaware of how to solve a quadratic equation, I suggest you make yourself aware with the quadratic formula and also how to use it. You can get that information on Wikipedia. Click here to go there.

- Start creating a new C++ (or cpp) file in Turbo C++ or any other program that you might want to use. Include the various header files that we would be required in this small application.
#include<iostream.h> #include<conio.h> #include<math.h>

- Next, create your main function and define the various floating point variables that we will be using. These variables include the co-efficients a,b, and c and the discriminant (d). Also, it has 2 variables that will store the solution. Also, get these values from the user’s keyboard.
void main() { clrscr(); float a,b,c,d,x1,x2; cout<<"\nA quadratic equation is given as: aX^2 + bX + c = 0"; cout<<"\nEnter a,b,c : "; cin>>a>>b>>c; getch(); }

- Now let us calculate the discriminant, and check if it is greater than 0 or not. If it is, we can directly apply the Quadratic formula, but if it is not then we need to multiply the discriminant by (-1) and then solve. Add this code after the “cin” line of the above code.
d=(b*b)-(4*a*c); if(d>=0){ x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); cout<<"\nThe roots of the quadratic equation are : "<<x1<<" and "<<x2; }else{ d=d*(-1); }

- Now, it is time to calculate the value of x1 and x2 when the discriminant is negative. Add these two lines after the previous code, but still inside the “
*else*“.cout<<"\nThe roots are imaginary!"; cout<<"\nThe roots of the quadratic equation are : " <<(-b/(2*a))<<" + "<<(sqrt(d)/(2*a))<<"i and "<<(-b/(2*a))<<" - "<<(sqrt(d)/(2*a));

- That’s all. We have completed the quadratic equation solver in C++. If you want you can download the complete code and compare it with that or just directly use the downloaded code.

### Download Source Code

Here is a sample output of the application we just built.

I hope this tutorial helps you to write an application that can very easily solve quadratic equations for you in future. Subscribe to Slash Coding to get all these tutorials delivered directly to you. You can like us on Facebook, Follow us on Twitter or Subscribe to RSS Feeds via your RSS Reader or via Email. See you Around!