Answer to Question #171081 in C++ for jordi moutchou

Question #171081

c++ program that find x when i input a, b and c. (using quadratic equation)


1
Expert's answer
2021-03-12T03:36:48-0500
#include <iostream>
#include <cmath>
using namespace std;
void Roots(float,float,float);
int main()
{
    float a, b, c;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;
    Roots(a,b,c);
    return 0;
}
void Roots(float a, float b, float c)
{
     float x1, x2, D, r_part, img_part;
     D = b*b - 4*a*c;
     if(D > 0)
     {
        x1 = (-b + sqrt(D)) / (2*a);
        x2 = (-b - sqrt(D)) / (2*a);
        cout << "\nRoots are real and different.";
        cout << "\nx1 = "<<x1;
        cout << "\nx2 = "<<x2;
    }


    else if (D == 0)
    {
        cout << "\nRoots are real and same.";
        x1 = -b/(2*a);
        cout << "\nx1 = "<<x1<<"\nx2 = " << x1 ;
    }


    else
    {
        r_part = -b/(2*a);
        img_part =sqrt(-D)/(2*a);
        cout << "\nRoots are complex and different.";
        cout << "\nx1 = " << r_part << "+" << img_part << "i";
        cout << "\nx2 = " << r_part << "-" << img_part << "i";
    }
}

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
APPROVED BY CLIENTS