c++ program that find x when i input a, b and c. (using quadratic equation)
#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";
}
}
Comments
Leave a comment