Detailed Print – CSKC
Sign In

Search

siteicon
Solved Problem!#CPP#2321 Back

Problem Statement - Output of a ternary operator operation

Write output of the following program using the ternary operation logic.

#include <iostream.h>
int main()
{
  int a=10,b=23;
  int c=a>b?a=b:b=a;
  cout<<c<<endl;
  return 0;
}

		 

Click to open popup

Run Output

10

Solution

Notes

int a=10,b=23;
int c=a>b?a=b:b=a;
Here looking into condition part of ternary operator a>b will be false as a is 10 and b is 23. Now based on operator rule the false past after the : operator will come into effect. This means that a will be copied to b (assignment operation always happens from right hand side copied to left hand side). Once this happens finally new value of b which is now 10 will be copied to c.

cout<<c<<endl;
This will print c which is 10.