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:24 | ISBN:9780321531346 | Edition: 7

Question

Write an if-else statement that outputs the word Warning provided that either the value of the variable temperature is greater than or equal to 100, or the value of the variable pressure is greater than or equal to 200, or both. Otherwise, the if-else statement outputs the word OK. The variables temperature and pressure are both of type int.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The following if-else statement that outputs the word 'Warning' provided that either the value of the variable temperature is greater than or equal to 100, or the value of the variable pressure is greater than or equal to 200, or both. Otherwise, the if-else statement outputs the word 'OK':

if ((temperature >= 100) || (preasure >= 200))
    cout << "Warning";
else
    cout <<"OK";

Complete executable program code:

#include <iostream>
using namespace std;

int main ()
{
    //Declare the variables temperature and
    //pressure are both of data type int.
    int temperature;
    int preasure;
    
    //Read  temperature value
    cout << "Enter a temperature value: ";
    cin >>temperature;
    //Read preasure value
    cout << "Enter a preasure value: ";
    cin >>preasure;
    
    //Use if-else statement that outputs the word Warning 
    //provided that either the value of the variable 'temperature'
    //is greater than or equal to 100, or the value of the variable
    //'pressure' is greater than or equal to 200, or both. Otherwise,
    //the if-else statement outputs the word 'OK'
    if ((temperature >= 100) || (preasure >= 200))
        cout << "Warning";
    else
        cout <<"OK";
    return 0;
}

Outputs:

Output 1:

Enter a temperature value: 100                                                                                                       
Enter a preasure value: 200                                                                                                          
Warning 

Output 2:

Enter a temperature value: 55                                                                                                        
Enter a preasure value: 45                                                                                                           
OK   

 

0 0

Discussions

Post the discussion to improve the above solution.