SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Self-test Exercises
Question:25 | ISBN:9780321531346 | Edition: 7

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.