Slash Coding

  • Android Development
  • CSS
  • Subscribe
  • About Us
  • Contact Us
You are here: Home / C++ / Quadratic Equation Solver in C++

Quadratic Equation Solver in C++

By Aneesh Bhatnagar

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.

  1. 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>
    
  2. 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();
    }
    
  3. 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);
    }
    
  4. 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));
    
  5. 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.Quadratic Equation Final Code

Download Source Code

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

Quadratic Equation Output Screenshot

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!

Did you enjoy this article?
Subscribe to our email alerts when we post something new so you don't miss out.

About Aneesh Bhatnagar

Aneesh Bhatnagar is a freelance web developer, who specializes in front-end website development with HTML5, CSS3 and jQuery. He believes in transforming the entire web into a Responsive Web. He also specializes in use of Bootstrap framework to get websites up and running.

Search Slash Coding

Follow Us

RSS Feeds Facebook Twitter Follow Google+

Categories

  • Android Development
  • C++
  • Developer Tips
  • Slash Coding
  • Web Development
    • CSS
    • JavaScript
    • jQuery
    • PHP

Recently Published

  • Moving Ahead from Front End and Android Development
  • How to Export a Project from Android Studio
  • What is jQuery? Let’s Answer this Common Question today!
  • Mobile App Prototyping : Pixate Studio
  • How to Create a Countdown Timer using jQuery

Subscribe to Email Alerts

Eager to learn more from Slash Coding?
Sign up for daily email updates when anything new comes up. Get it straight in your inbox.

Follow Us on Social Media

RSS Feeds Facebook Twitter Follow Google+

Copyright © 2025 · Slash Coding · Powered by the Genesis Framework
Sitemap | Privacy Policy | About | Contact

×
Get Notified of the new Tutorials when they are posted!
Never miss any article update from Slash Coding. Subscribe to email alerts today!