Write a C++ program to find out the roots of a quadratic equation.


Write a C++ program to find out the roots of a quadratic equation.
Write a C++ program to find out the roots of a quadratic equation.


A quadratic equation is a second-order polynomial equation in a single variable x. It can be written in the standard form:


ax^2 + bx + c = 0


where a, b, and c are constants, and x is the variable. The quadratic formula is a well-known formula that gives the solutions (roots) of a quadratic equation. It is given by:


x = (-b ± sqrt(b^2 - 4ac)) / 2a


In this article, we will discuss how to write a C++ program to find the roots of a quadratic equation using the quadratic formula.


Algorithm:


Declare three variables a, b, and c of type double.


Read the values of a, b, and c from the user.


Calculate the discriminant of the quadratic equation using the formula b^2 - 4ac.


Check the value of the discriminant. If it is greater than zero, the equation has two distinct real roots. If it is equal to zero, the equation has one real root. If it is less than zero, the equation has two complex roots.


Calculate the roots of the quadratic equation using the quadratic formula.


Print the roots of the quadratic equation.


C++ Program:



#include <iostream>

#include <cmath>

using namespace std;


int main()

{

   double a, b, c, discriminant, root1, root2;


   cout << "Enter the coefficients a, b, and c: ";

   cin >> a >> b >> c;


   discriminant = b * b - 4 * a * c;


   if (discriminant > 0)

   {

      root1 = (-b + sqrt(discriminant)) / (2 * a);

      root2 = (-b - sqrt(discriminant)) / (2 * a);

      cout << "The roots are real and distinct." << endl;

      cout << "Root 1 = " << root1 << endl;

      cout << "Root 2 = " << root2 << endl;

   }

   else if (discriminant == 0)

   {

      root1 = -b / (2 * a);

      cout << "The roots are real and equal." << endl;

      cout << "Root 1 = Root 2 = " << root1 << endl;

   }

   else

   {

      double realPart = -b / (2 * a);

      double imaginaryPart = sqrt(-discriminant) / (2 * a);

      cout << "The roots are complex and different."  << endl;

      cout << "Root 1 = " << realPart << "+" << imaginaryPart << "i" << endl;

      cout << "Root 2 = " << realPart << "-" << imaginaryPart << "i" << endl;

   }


   return 0;

}


Explanation:


We start by declaring the variables a, b, and c of type double. These variables will hold the coefficients of the quadratic equation. We also declare three more variables discriminant, root1, and root2.


Next, we prompt the user to enter the values of a, b, and c using the cout and cin statements. These values are read into the variables a, b, and c.


We calculate the discriminant of the quadratic equation using the formula b^2 - 4ac and store it in the variable discriminant.


We then use an if-else statement to check the value of the discriminant. If it is greater than zero, the equation has two distinct real roots. We use the quadratic formula to calculate the roots and store them in the variables root1 and root2. We then print the roots using cout statements.