41.5k views
3 votes
Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "non-negative" otherwise. Ex: If userVal is -9, output is:

-9 is negative. PLEASE USE C++

User Dannyla
by
4.3k points

1 Answer

1 vote

Answer:

PROGRAMMING APPROACH:

  • Define the necessary header file using namespace.
  • Define the main() method.
  • Declare variable inside the function().
  • Print result.

Step-by-step explanation:

Required C++ Code:

#include<iostream>

#include<string>

using namespace std;

int main()

{

string condStr;

int userVal;

cin>>userVal;

condStr=(userVal<0)?("negative"):("non-negative");

cout<<userVal<< " is "<< condStr <<"."<<endl;

}

OUTPUT:

-2

-2 is negative.

User Stanislav Yaglo
by
4.2k points