Ternary operator in C++ by R4R Team

What is ternary operator?
Syntax-
expression?value1:value2;

-Depend on the expression result that it is true or false it may return different results.
-it is conditional operator.
-If Expression is True then it will return value1.
-If Expression is False then it will return value2.

Find maximum of two number:

program:

#include < iostream>
using namespace std;
int main()
{
int a,b,max;
cout<<"Enter two numbersn";
cin>>a>>b;
max=a>b?a:b;
cout<<"Maximum is: "<< max;
return 0;
}


output-

Enter two numbers
3 7
Maximum is: 7




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!