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

 
#include < stdio.h >

int main()
{
    int a, b, c, big;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &a, &b, &c);

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

    printf("Biggest is %d\n", big);

    return 0;
}

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

 
#include < stdio.h >

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

    printf("Enter 3 numbers\n");
    scanf("%d %d %d", &a, &b, &c);

    if( a > b && a > c)
    {
        printf("%d is biggest\n", a);
    }
    else
    {
        if( b > c )
        {
            printf("%d is biggest\n", b);
        }
        else
        {
            printf("%d is biggest\n", c);
        }
    }

    return 0;
}

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

 
#include < stdio.h >

int main()
{
    int a, b, c, big;

    printf("Enter 3 numbers\n");
    scanf("%d %d %d", &a, &b, &c);

    if( a > b && a > c)
    {
        big = a;
    }
    else
    {
        if( b > c )
        {
            big = b;
        }
        else
        {
            big = c;
        }
    }

    printf("Biggest of 3 number is %d\n", big);

    return 0;
}

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