 # Answered! Write a C++ program that will announce the number of real roots of the equation for various user-provided values of a, b, and c, and then computes and announces the real roots (if any)….

Write a C++ program that will announce the number of real roots of the equation for various user-provided values of a, b, and c, and then computes and announces the real roots (if any).

1. Prompt the user for three values: a, b, and c.

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Write a C++ program that will announce the number of real roots of the equation for various user-provided values of a, b, and c, and then computes and announces the real roots (if any)….
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

Please enter an integer value for a: ->

Please enter an integer value for b: ->

Please enter an integer value for c: ->

2. Compute the value of the discriminant and announce the results as they pertain to the number of roots.

Remember that the discriminant of the quadratic equation is the value of the arithmetic expression under the radical:

b2 – 4 a c

Clearly, you must use the arithmetic operator for multiplication and the assignment operator to store the computed value into a variable. The value if the discriminant is: positive – two real roots zero – one real root negative – no real roots. Once you have computed this value, announce the number of roots you will compute.

3. Compute and announce the roots of the equation, if any. To compute a square root use the sqrt() function from the math library so include the math library: #include.

Later in your program you can do this: double discriminant = … (1) double root = sqrt(discriminant); (2) If discriminant were assigned 25 when its value was computed at (1), then root will have the value 5 after (2) executes!!! Isn’t that great??

Code:

#include <iostream>
#include <cmath>
using namespace std;

// Program to demonstrate Quadratic root equation
int main()
{
double a, b, c;
double discriminant, root1, root2;

// Taking input from user
cout << “Please enter an integer value for a:-> “<<endl;
cin >> a;
cout << “Please enter an integer value for b:-> “<<endl;
cin >> b;
cout << “Please enter an integer value for c:-> “<<endl;
cin >> c;

// Calculate discriminant from the given values of a, b, c
discriminant = b * b – 4 * a * c;
// if discriminant > 0 will calculate two roots
if (discriminant > 0) {
root1 = (-b + sqrt (discriminant) ) / 2 * a;
root2 = (-b – sqrt(discriminant) ) / 2 * a;
cout << “The equation has two roots: ” << root1 << ” ” << root2 << endl;
}
// if discriminant is negative the equation has no real roots
else if (discriminant < 0) {
cout << “The equation has no real roots.” << endl;
}
// if discriminant is zero there is only root
else {
root1 = -b / 2 * a;
cout << “The equation has one root: “<< root1 << endl;
}

return 0;
}

Execution and output:
Please enter an integer value for a:->
10
Please enter an integer value for b:->
20
Please enter an integer value for c:->
5
The equation has two roots: -29.2893 -170.711

Please enter an integer value for a:->
1
Please enter an integer value for b:->
2
Please enter an integer value for c:->
3
The equation has no real roots.

Please enter an integer value for a:->
1
Please enter an integer value for b:->
2
Please enter an integer value for c:->
1
The equation has one root: -1