Biggest of 3 Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements and Ternary Operator.

Before watching this video, please make sure to watch and understand this short video: Find Biggest of 3 Numbers: C++

Full Source Code

#include
#include

void main()
{
 int a, b, c;
 clrscr();

 cout< <"Enter 3 no's\n";
 cin>>a>>b>>c;

 int big =  ( a>b && a>c )?a:(b>c?b:c);
 /*
 if( a > b && a > c )
   big = a;
 else if( b > c )
   big = b;
 else
   big = c;
 */ cout< <"\nAmong "<

In this program we take 3 integer values from the user and using ternary operator decide the biggest of 3 numbers.

 int big =  ( a>b && a>c )?a:(b>c?b:c);

if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Video Tutorial: Biggest of 3 Integer Numbers



YouTube Link: https://www.youtube.com/watch?v=gfmqT4PwfY0 [Watch the Video In Full Screen.]



Output
Enter 3 no's
10
20
30
Among 10, 20, 30 Biggest is 30

View Comments