Header Ads

Conditional Operator in C

Conditional operator in C use to reduce the number of lines for checking a condition or use to skip the if statements by using a single line coding
Conditional operator uses two symbols for together for indication ‘ ? ‘ and ‘ : ‘ it has different meaning
? à Then
: à If not then
We write conditional operators in opening and closing bracket i.e. (if condition? this: this) and end with semi-colon ;

Take a example of smallest of three number using conditional operator:

#include<stdio.h>
#include<conio.h>
main()
{
    int n1,n2,n3,s;
    printf("Enter the three numbers :\n");
    scanf("%d %d %d",&n1,&n2,&n3);
    s=(n1<n2&&n1<n3?n1:n2<n3?n2:n3);
    printf("Smallest of given number is %d",s);
    getch();

}

Output 

Enter the three numbers :
25
65
11
Smallest of given number is 11_

Related Program:
  1. Largest of three numbers using conditional operator.
  2. Smallest of three numbers using conditional operator.

No comments