Conditional operator: This operator validates the condition and execution corresponding statement. It is also called Ternary operator.
Program to display Biggest of 2 numbers:
#include <stdio.h> int main(){ int a, b; printf(“Enter 2 numbers : \n”); scanf(“%d%d”, &a, &b); a>b ? printf(“A is big \n”) : printf(“B is big \n”); return 0; } |
Program to check the given number is Even or not:
#include <stdio.h> int main(){ int n; printf(“Enter number : \n”); scanf(“%d”, &n); n%2==0 ? printf(“Even \n”) : printf(“Not even \n”); return 0; } |
We can define conditional statement inside another conditional statement:
Program to check the 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); big = a>b && a>c ? a : b>c ? b : c; printf(“Big one is : %d \n”, big); return 0; } |