Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), it\'s

ID: 3874220 • Letter: F

Question

For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), it's roots is given by the formula:-

The value of the discriminant (b2-4ac) determines the nature of roots. Write a C++ program that reads the values of a, b and c from the user and performs the following:-

a. If the value of the discriminant is positive, program should print out that the equation has two real roots and prints the values of the two roots.

b. If the discriminant is equal to 0, the roots are real and equal.

c. if the value of the discriminant is negative, then the equation has two complex roots.

Sample Output

Enter values for a, b and c:- 2 10 3
Discriminant = 76

Equation has two real roots
Roots are as follows:-
x1 = -0.320551
x1 = -0.320551

Any character to end:-

Explanation / Answer

Given below is the code for the question.
Please don't forget to rate the answer if it was helpful. Thank you


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
double discri;
double x1, x2;
  
cout << "Enter values for a, b and c: ";
cin >> a >> b >> c;
  
discri = b * b - 4 * a * c;
cout << "Discriminant: " << discri << endl;
if(discri == 0)
{
x1 = -b / (2*a);
cout << "Equation has equal roots " << endl;
}
else if(discri < 0)
cout << "Equation has complex roots" << endl;
else
{
cout << "Equation has 2 real roots" << endl;
x1 = (-b + sqrt(discri)) / (2 * a);
x2 = (-b - sqrt(discri)) / (2 * a);
  
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;

}

return 0;
}


output
Enter values for a, b and c: 2 10 3
Discriminant: 76
Equation has 2 real roots
x1 = -0.320551
x2 = -4.67945

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote