Biggest of 3 Numbers Using Ternary Operator: C

Lets find biggest of 3 numbers using ternary operator / conditional operator. This program is an example for nested ternary operator.

Related Read:
Ternary Operator / Conditional Operator In C
Logical Operators In C

To find biggest of Three numbers using if-else control structure:
Biggest of 3 Numbers: C

Source Code: Biggest of Three Numbers using ternary operator: C

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b, c, big;  
  7.   
  8.     printf("Enter 3 numbers\n");  
  9.     scanf("%d%d%d", &a, &b, &c);  
  10.   
  11.     big = (a>b && a>c) ? (a) : ( (b>c)?(b):(c) );  
  12.   
  13.     printf("Biggest is %d\n", big);  
  14.   
  15.     return 0;  
  16. }  

Output 1:
Enter 3 numbers
1
2
3
Biggest is 3

Output 2:
Enter 3 numbers
1
3
2
Biggest is 3

Output 1:
Enter 3 numbers
3
2
1
Biggest is 3

In above source code, if a is bigger than b as well as c, then value of a is returned and stored in variable big orelse exression3 gets executed – where we check if b is greater than c.

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Nested Ternary Operator

In our source code, we nest Ternary Operator inside expression3 to find biggest among b and c.

Note:
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.

Biggest of 3 Numbers Using Ternary Operator: C Program


[youtube https://www.youtube.com/watch?v=UEyCWg_pMAA]

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


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of 3 Numbers: C

Lets write C program to find biggest of 3 numbers, using nested if else statement.

Related Read:
Nested if else Statement In C
Relational Operators In C

Biggest of 3 Numbers Using Ternary Operator: C

C Program To Find Biggest of 3 numbers

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b, c;  
  7.   
  8.     printf("Enter 3 numbers\n");  
  9.     scanf("%d %d %d", &a, &b, &c);  
  10.   
  11.     if( a > b && a > c)  
  12.     {  
  13.         printf("%d is biggest\n", a);  
  14.     }  
  15.     else  
  16.     {  
  17.         if( b > c )  
  18.         {  
  19.             printf("%d is biggest\n", b);  
  20.         }  
  21.         else  
  22.         {  
  23.             printf("%d is biggest\n", c);  
  24.         }  
  25.     }  
  26.   
  27.     return 0;  
  28. }  

Output 1:
Enter 3 numbers
1
2
3
3 is biggest

Output 2:
Enter 3 numbers
10
18
15
18 is biggest

Here first we check if a is greater than b. If yes, then we display a as big. Else b or c is biggest. So now we check if b is greater than c, if yes, we display b as biggest else c is biggest. Nested if else condition works great for programs like this.

Biggest of 3 numbers: C Program


[youtube https://www.youtube.com/watch?v=iy–Q0cLnfc]

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


C Program To Find Biggest of 3 numbers

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b, c, big;  
  7.   
  8.     printf("Enter 3 numbers\n");  
  9.     scanf("%d %d %d", &a, &b, &c);  
  10.   
  11.     if( a > b && a > c)  
  12.     {  
  13.         big = a;  
  14.     }  
  15.     else  
  16.     {  
  17.         if( b > c )  
  18.         {  
  19.             big = b;  
  20.         }  
  21.         else  
  22.         {  
  23.             big = c;  
  24.         }  
  25.     }  
  26.   
  27.     printf("Biggest of 3 number is %d\n", big);  
  28.   
  29.     return 0;  
  30. }  

Output:
Enter 3 numbers
500
550
555
Biggest of 3 number is 555

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find Biggest of 3 Numbers: C++

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

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no\n";
cin>>a>>b>>c;
 
int big;
 
if( a > b )
 big = a;
else
{
 if( b > c )
  big = b;
 else
  big = c;
}
cout< <"\nBiggest is: "<<big;
getch();
}

In this program we get three integer variables from the user.
If value of variable a is bigger, then we store it inside the variable big; else we compare b with c. Now will store the biggest number inside the variable big.
Video Tutorial: Biggest of 3 Integer Numbers


[youtube https://www.youtube.com/watch?v=YRkCpS_GMFw]

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



Output
Enter 3 no’s
404
501
301
Biggest is: 501