Conditional Operator in C-Language :

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,

expression 1 ? expression 2 : expression 3;

If expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3.

Let us understand this with the help of a few examples:

1. a) int x,y;

scant ( “%d”, &x ) ;

y = (x > 5? 3:4);

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.

The equivalent if statement will be,

if (x>5)

y = 3;

else

y = 4;

Write a C Program to find largest of two number using conditional operator.

#include<stdio.h>
void main()
{
int no1,no2,big;
printf("Enter first number :");
scanf("%d",&no1);
printf("Enter Second number :");
scanf("%d",&no2);
big=no1>no2?no1:no2;
printf("\n The biggest number is %d",big);
}

OR

Write a C Program to find largest of two number using conditional operator.

#include<stdio.h>
void main()
{
int no1,no2;
printf("Enter first number :");
scanf("%d",&no1);
printf("Enter Second number :");
scanf("%d",&no2);
printf("/nThe biggest number is %d",no1>no2?no1:no2);
}

Write a C Program to find largest of three numbers using conditional operator.

#include<stdio.h>
void main()
{
int no1,no2,no3,big;
clrscr();
printf("Enter first number :");
scanf("%d",&no1);
printf("Enter Second number :");
scanf("%d",&no2);
printf("Enter Third number :");
scanf("%d",&no3);
big=no1>no2?(no1>no3?no1:no3):no2>no3?no2:no3;
printf("The Biggest number is : %d ",big);
}