fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void somadhan()
  4. {
  5. //Syntex of ternary operator
  6. //condition? expression_if_true : expression_if_false;
  7.  
  8. //Greater between two integer.
  9. int a=10, b=-12;
  10.  
  11. int greater = (a>b) ? a : b ;
  12.  
  13. //ternary operator--> (a>b) ? a : b ;
  14.  
  15. cout<<"Greater number between "<< a<<" & "<<b <<" is: "<<greater<<"\n";
  16.  
  17. //checking positive, neqative or zero;
  18.  
  19. int num=-11;
  20.  
  21. string check= (num>0) ? "Positive" :(num==0) ? "Zero" : "Negative" ;
  22.  
  23. //Nested ternary operator--> (num>0) ? "Positive" :(num==0) ? "Zero" : "Negative" ;
  24.  
  25. cout<<num<<" is a "<<check<<" number."<<"\n";
  26.  
  27. //Checking even odd
  28. string result= (num%2==0) ? "even" : "odd" ;
  29.  
  30. //ternary operator --> (num%2==0) ? "even" : "odd" ;
  31.  
  32. cout<<num<<" is an "<<result<<" number"<<"\n";
  33.  
  34. }
  35.  
  36. int main()
  37. {
  38. ios::sync_with_stdio(false);
  39. cin.tie(0), cout.tie(0);
  40.  
  41. somadhan();
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Greater number between 10 & -12 is: 10
-11 is a Negative number.
-11 is an odd number