Consider a quadratic expression, say x2 − x − 2 Describing where this quadratic is positive (that is, greater than 0), involves describing a set of numbers that are either less than the smaller root (which is −1) or greater than the larger root (which is +2). Write a C++ Boolean expression that is true when this formula has positive values.
Program code:
#include <iostream>
using namespace std;
int main() {
double x; // Variable for input
// Prompt the user to enter a value for x
cout << "Enter a value for x: ";
cin >> x;
// Evaluate the quadratic expression x^2 - x - 2
double quadratic = x * x - x - 2;
// Check if the quadratic expression is positive
bool isPositive = quadratic > 0;
// Display the result
cout << "The quadratic expression is positive: " << std::boolalpha << isPositive << std::endl;
return 0;
}
Executed Output:
Enter a value for x: 5
The quadratic expression is positive: true