C++ Program to Find Roots of a Quadratic Equation

C++ Program to Find Roots of a Quadratic Equation

Quadratic equations are one of the fundamental topics in algebra and appear in mathematics, physics, and computer programming. A quadratic equation has the general form ax² + bx + c = 0, where a, b, and c are constants and a ≠ 0. Solving such equations programmatically allows beginners to practice arithmetic operations, conditional statements, and mathematical functions in C++.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Finding Roots Using the Standard Formula

This program calculates the roots of a quadratic equation using the discriminant formula. It identifies whether the roots are real and distinct, real and equal, or complex.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double a, b, c;

    cout << "Enter coefficient a: " << endl;
    cin >> a;

    cout << "Enter coefficient b: " << endl;
    cin >> b;

    cout << "Enter coefficient c: " << endl;
    cin >> c;

    if(a == 0) {

        cout << "Coefficient a cannot be 0 for a quadratic equation." << endl;
        return 0;

    }

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

    if(discriminant > 0) {

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

        cout << "The equation has two distinct real roots: " << root1 << " and " << root2 << endl;

    } else if(discriminant == 0) {

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

        cout << "The equation has one real root: " << root << endl;

    } else {

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

        cout << "The equation has two complex roots: "
             << realPart << " + " << imaginaryPart << "i and "
             << realPart << " - " << imaginaryPart << "i" << endl;

    }

    return 0;

}

This program calculates the discriminant b² – 4ac to determine the nature of the roots. Positive discriminant gives two real roots, zero gives one real root, and negative gives complex roots. Beginners can learn how to use if-else statements and the sqrt() function to handle different scenarios effectively.

Program 2: Using a Function to Compute Roots

Encapsulating the logic in a function improves modularity and makes it easier to solve multiple equations. This program demonstrates how functions simplify the structure of your code.

#include <iostream>
#include <cmath>

using namespace std;

void findRoots(double a, double b, double c) {

    if(a == 0) {

        cout << "Coefficient a cannot be 0 for a quadratic equation." << endl;
        return;

    }

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

    if(discriminant > 0) {

        double root1 = (-b + sqrt(discriminant)) / (2*a);
        double root2 = (-b - sqrt(discriminant)) / (2*a);
        cout << "Two distinct real roots: " << root1 << " and " << root2 << endl;

    } else if(discriminant == 0) {

        double root = -b / (2*a);
        cout << "One real root: " << root << endl;

    } else {

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

        cout << "Two complex roots: "
             << realPart << " + " << imaginaryPart << "i and "
             << realPart << " - " << imaginaryPart << "i" << endl;

    }

}

int main() {

    double a, b, c;

    cout << "Enter coefficient a: " << endl;
    cin >> a;

    cout << "Enter coefficient b: " << endl;
    cin >> b;

    cout << "Enter coefficient c: " << endl;
    cin >> c;

    findRoots(a, b, c);

    return 0;

}

Using a function separates the computation from input/output handling. Beginners can see how modular programming enhances readability and reusability, especially when solving multiple quadratic equations.

Program 3: Interactive Program to Solve Multiple Quadratic Equations

This version allows the user to solve multiple quadratic equations in one session, using a loop to repeat the process. It reinforces the use of functions, loops, and conditionals together.

#include <iostream>
#include <cmath>

using namespace std;

void findRoots(double a, double b, double c) {

    if(a == 0) {

        cout << "Coefficient a cannot be 0 for a quadratic equation." << endl;
        return;

    }

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

    if(discriminant > 0) {

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

        cout << "Two distinct real roots: " << root1 << " and " << root2 << endl;

    } else if(discriminant == 0) {

        double root = -b / (2*a);
        cout << "One real root: " << root << endl;

    } else {

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

        cout << "Two complex roots: "
             << realPart << " + " << imaginaryPart << "i and "
             << realPart << " - " << imaginaryPart << "i" << endl;

    }

}

int main() {

    char choice;

    do {

        double a, b, c;

        cout << "Enter coefficient a: " << endl;
        cin >> a;

        cout << "Enter coefficient b: " << endl;
        cin >> b;

        cout << "Enter coefficient c: " << endl;
        cin >> c;

        findRoots(a, b, c);

        cout << "Do you want to solve another equation? (y/n): " << endl;
        cin >> choice;

    } while(choice == 'y' || choice == 'Y');

    return 0;

}

This program teaches beginners how to combine loops, functions, and input validation to create interactive applications. It’s a practical approach to practicing problem-solving and structuring code efficiently.

Frequently Asked Questions (FAQ)

Here are some common questions about solving quadratic equations in C++:

Q1: What determines the type of roots in a quadratic equation?
The discriminant b² – 4ac decides the nature of roots: positive for two real roots, zero for one real root, and negative for complex roots.

Q2: Can a quadratic equation have complex roots?
Yes, when the discriminant is negative, the equation has complex roots with real and imaginary parts.

Q3: What if the coefficient a is 0?
If a = 0, the equation becomes linear, not quadratic, and requires a different approach to solve.

Q4: Why use functions in these programs?
Functions make code modular, reusable, and easier to maintain, especially for solving multiple equations.

Q5: Can this program handle decimal coefficients?
Yes, using the double data type ensures accurate handling of fractional or decimal numbers.

Conclusion

Finding the roots of a quadratic equation in C++ is a fundamental exercise for beginners, teaching them arithmetic operations, conditionals, functions, and loops. We explored three approaches: a direct calculation using the discriminant, a function-based approach for modularity, and an interactive program for multiple equations. Practicing these techniques strengthens both programming skills and understanding of algebra.

Additional & References

Learning to solve quadratic equations programmatically helps build a strong foundation for algebraic computation and numerical methods. Beginners are encouraged to experiment with complex coefficients, graphical representations, or extending the program to cubic equations.

Scroll to Top