Write a c++ program to evaluate the following expression
X1= (-b+[√b^2-4ac]) /2a
and 2nd is
X2= [-b+(-√b^2-4ac)]\2a
Answers
Answer:
The idea is to take the values of a,b,c and then calculate the root of individual x1 and x2 . take a condition that if the square part is greater than zero then it will show the answer other wise both the answers will be same
Explanation:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, rootanswer;
cout << "Enter the values of a, b and c: ";
cin >> a >> b >> c;
rootanswer = b*b - 4*a*c;
if (rootanswer > 0) {
x1 = (-b + sqrt(rootanswer)) / (2*a);
x2 = (-b - sqrt(rootanswer)) / (2*a);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (rootanswer == 0) {
cout << "the aanswers of the all the roots are same " << endl;
x1 = (-b + sqrt(rootanswer)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
return 0;
}
C++ program to evaluate the expression given:
#include <math.h>
void main()
{
double a,b,c, discriminant, root1, root2, realPart, imaginaryPart;
cout<<"Enter coefficients a, b and c respectively: ";
cin>>a;
cin>>b;
cin>>c;
discriminant = b*b-4*a*c;
if (discriminant > 0)
{
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
cout<<"root1 =” <<root 1;
cout<<”root2 = ”<< root2;
}
return;
}