2- Use a conditional operator to fill in the underlined part of the "cout" statement to make the following code prints either “is equal to C++” or “is not equal to C++” depending on value of the string. Put your entire code only inside the blank spaces in parentheses and do not modify other parts of the code. (4 pts):
string st;
cout << "Enter a string:";
cin >> st;
cout << "String " << st <<
(........................................................................................) ;
3- Given this code (3 pts)
int n = 5;
if (6 < n < 1)
cout << "n is greater 6 and less than 1";
else
cout << "n is not greater 6 and less than 1";
What is the output? Why?
2.
The complete source code is given below:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string st;
cout << "Enter a string:";
cin >> st;
cout << "String " << st <<((st.compare("is equal
to C++")==0)?" is equal to C++":" is not equal to C++");
return 0;
}
OUTPUT:
Enter a string:is not equal to C++
String is is not equal to C++
2.
OUTPUT:
n is greater 6 and less than 1
Explanation:
//variable declarationa and initialization
int n = 5;
//the result of 6<n will be false or 0
//0 < 1 will be true
if (6 < n < 1)
cout << "n is greater 6 and less than 1";
else
cout << "n is not greater 6 and less than 1";
So, the if() condition will be true and the if() block will execute.
Get Answers For Free
Most questions answered within 1 hours.