C++ Program to Find the Roots of a Quadratic Equation

A quadratic equation is a second-degree polynomial of the form ax2+bx+c=0, where a, b, and c are constants, and a≠0. The solutions to this equation, known as the roots, can be found using the quadratic formula:

x = \frac{{-b \pm \sqrt{{b^2 - 4ac}}}}{2a}

​​ Depending on the discriminant

\Delta = b^2 - 4ac

the equation can have:

  • Two distinct real roots (Δ>0)
  • One real root (Δ=0)
  • Two complex roots (Δ<0)

Understanding the Quadratic Formula

The quadratic formula provides the roots of the quadratic equation. The discriminant (Δ\DeltaΔ) determines the nature of the roots:

  • If Δ>0, the equation has two distinct real roots.
  • If Δ=0, the equation as one real root (a repeated root).
  • If Δ<0 the equation has two complex roots.

C++ Program to Find the Roots of a Quadratic Equation

C++
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double a, b, c;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;
    
    if (a == 0) {
        cout << "Coefficient 'a' cannot be zero in a quadratic equation." << endl;
        return 1; // Exit the program
    }
    
    double discriminant = b * b - 4 * a * c;
    double sqrt_val = sqrt(abs(discriminant));
    
    if (discriminant > 0) {
        cout << "The equation has two distinct real roots:" << endl;
        cout << "Root 1: " << (-b + sqrt_val) / (2 * a) << endl;
        cout << "Root 2: " << (-b - sqrt_val) / (2 * a) << endl;
    } else if (discriminant == 0) {
        cout << "The equation has one real root:" << endl;
        cout << "Root: " << -b / (2 * a) << endl;
    } else {
        cout << "The equation has two complex roots:" << endl;
        cout << "Root 1: " << -b / (2 * a) << " + " << sqrt_val / (2 * a) << "i" << endl;
        cout << "Root 2: " << -b / (2 * a) << " - " << sqrt_val / (2 * a) << "i" << endl;
    }
    
    return 0;
}

Output

Example 1: Two Distinct Real Roots

C++
Enter coefficients a, b and c: 1 -3 2
The equation has two distinct real roots:
Root 1: 2
Root 2: 1

Example 2: One Real Root

C++
Enter coefficients a, b and c: 1 -2 1
The equation has one real root:
Root: 1

Example 3: Two Complex Roots

C++
Enter coefficients a, b and c: 1 2 5
The equation has two complex roots:
Root 1: -1 + 2i
Root 2: -1 - 2i

Explanation:

  1. Input Handling:
    • The program prompts the user to enter the coefficients a, b, and c.
    • It checks if aaa is zero, which would make the equation non-quadratic. If aaa is zero, the program exits with an error message.
  2. Discriminant Calculation:
    • The discriminant is calculated as Δ=b2−4ac.
    • The square root of the absolute value of the discriminant is computed for further calculations.
  3. Root Calculation:
    • If the discriminant is positive (Δ>0, the equation has two distinct real roots, calculated and displayed using the quadratic formula.
    • If the discriminant is zero (Δ=0), the equation has one real root, calculated and displayed.
    • If the discriminant is negative (Δ<0), the equation has two complex roots, calculated and displayed in the form of complex numbers.

Conclusion

Finding the roots of a quadratic equation using C++ involves understanding the quadratic formula and handling different cases based on the discriminant. This program demonstrates how to implement these concepts in a structured and efficient manner. By using this program, you can quickly and accurately determine the nature and values of the roots for any quadratic equation.