C++ Program to Solve a Quadratic Equation

C++ Program to Solve a Quadratic Equation

Quadratic equations are a fundamental part of algebra, appearing in physics, engineering, and computer programming. A quadratic equation has the form ax² + bx + c = 0, where a, b, and c are coefficients and a ≠ 0. Solving quadratic equations programmatically helps beginners practice arithmetic operations, conditionals, and the use of 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: Solve Quadratic Equation Using the Standard Formula

This program calculates the roots of a quadratic equation using the discriminant formula. It handles all cases, including real and distinct roots, real and equal roots, and complex roots.

#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 first calculates the discriminant b² – 4ac, which determines the type of roots. Positive discriminant gives two real roots, zero gives one real root, and negative gives complex roots. Beginners can learn how to use conditionals and the sqrt() function to handle all possible scenarios in quadratic equations.

Program 2: Using a Function to Solve Quadratic Equations

Encapsulating the logic in a function makes the program modular. This approach is useful when solving multiple equations or integrating the logic into larger programs.

#include <iostream>
#include <cmath>

using namespace std;

void solveQuadratic(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;

    solveQuadratic(a, b, c);

    return 0;

}

Using a function makes the code cleaner and easier to maintain. Beginners can understand the benefits of modular programming while focusing on the computation separately from input/output operations.

Program 3: Interactive Program for Multiple Quadratic Equations

This version allows users to solve multiple quadratic equations in one session, demonstrating the use of loops along with functions and conditionals.

#include <iostream>
#include <cmath>

using namespace std;

void solveQuadratic(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;

        solveQuadratic(a, b, c);

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

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

    return 0;

}

This interactive program combines functions, loops, and user input, giving beginners experience in creating programs that handle repeated tasks efficiently while maintaining clarity and structure.

Frequently Asked Questions (FAQ)

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

Q1: What is the discriminant in a quadratic equation?
The discriminant is b² – 4ac. It determines 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, if the discriminant is negative, the roots are complex numbers with real and imaginary parts.

Q3: What happens if coefficient a is 0?
If a = 0, the equation is no longer quadratic. It becomes a linear equation and must be solved differently.

Q4: Why use functions to solve quadratic equations?
Functions make the program modular, reusable, and easier to maintain, especially when solving multiple equations.

Q5: Can this program handle decimal coefficients?
Yes, using double ensures that the program can handle fractional or decimal numbers accurately.

Conclusion

Solving quadratic equations in C++ is an essential exercise for beginners to practice conditionals, loops, and mathematical operations. We explored three approaches: a simple arithmetic method, a modular function-based approach, and an interactive version for multiple equations. Practicing these techniques helps beginners strengthen both programming skills and understanding of algebra.

Additional & References

Learning to solve quadratic equations programmatically lays the groundwork for more advanced topics, such as polynomial operations and numerical methods. Beginners are encouraged to extend these programs to handle systems of equations or to create graphical solutions.

Scroll to Top