Biggest of 3 numbers using Ternary Operator

This “Biggest of 3 Numbers using Ternary Operator” post explains what is conditional operator, how to use conditional operator, how to write biggest of 3 numbers program using conditional operator in different ways.


#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter 3 integers : “);
scanf(“%d%d%d”, &a , &b , &c);
a>b && a>c ? printf(“A”) : b>c ? printf(“B”) : printf(“C”);
}


#include<stdio.h>
void main()
{
int a,b,c , big ;
printf(“Enter 3 integers : “);
scanf(“%d%d%d”, &a , &b , &c);
big = a>b && a>c ? a : b>c ? b : c;
printf(“Big : %d \n”, big);
}


#include<stdio.h>
void main()
{
int x ;
x = 10 > 20 ? 10 : 20 > 30 ? 20 : 30 ;
printf(“x : %d \n”, x);
}


#include<stdio.h>
void main()
{
int x ;
x = 20 > 10 ? 30 > 20 ? 30 : 20 : 10 ;
printf(“x : %d \n”, x);
}


 

Share this