Answer to Question #151748 in C++ for Nadia zafar

Question #151748
If coefficients of quadratic equations are known then write a program to find the roots of quadratic equation in closed form.
Also specify the nature of roots(the roots can be real, imaginary or equal)
Don't ask user to input any value.
Don't use these variables names a, b, c, d, disc.
1
Expert's answer
2020-12-17T08:56:41-0500
#include <iostream>
#include <cmath>
using namespace std;
int main () {
    // we declare an array for store coefficients of quadratic equation
    // for example x^2+5x+6=0
    int coefficients[3] = {1, 5, 6};
    float root1, root2;
    float discriminant;
    discriminant = pow(coefficients[1], 2) - 4 * coefficients[0]*coefficients[2];
    if (discriminant > 0) {
        cout << "The quadratic equation has two real numbers and they are: " << endl;
        root1 = (-coefficients[1] + sqrt(discriminant)) / 2 * coefficients[0];
        root2 = (-coefficients[1] - sqrt(discriminant)) / 2 * coefficients[0];
        cout << root1 << " " << root2;
    }
    else if (discriminant == 0) {
        cout << "The quadratic equation has two identical roots and it is: " << endl;
        cout << -coefficients[1] / 2. * coefficients[0];
    }
    else {
        cout << "The quadratic equation hasn't roots";
    }


    return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog