Find the Roots of a Quadratic Equation

// Quadratic equation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "cmath"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       double a, b, c, root1, root2;
       cout << "Enter the coefficients of quadratic equation a, b and c: " << "\n";
       cin >> a >> b >> c;
       double determinant;
       determinant = b * b - 4 * a * c;
       if(determinant > 0)
       {
             double root = sqrt(determinant);
             root1 = 0.5 * (-b + determinant) / a;
             root2 = 0.5 * (-b - determinant) / a;
             cout << "The solutions of quadratic equation are: " << root1 << "and" << root2 <<"\n";
       }
       else if(determinant < 0)
       {
             double root = sqrt(-determinant);
             double real_part = -0.5 * b / a;
             double imag_part = 0.5 * root / a;
             cout << "There are two complex solutions: " << real_part <<
                    " + i * " << imag_part << " and " << real_part << " - i * " <<
                    imag_part << "\n";
       }
       else {
             cout << "Both solutions are: " << -0.5*b/a << "\n";
       }
       system("pause");
       return 0;
}

Solutions are real


Solutions are complex




One real solution

Share on Google Plus