Generally if we need to evaluate the largest of two numbers we need to use if statement, but there is an alternative for evaluating this, that is using a Conditional Operator. A Conditional operator (? : ) is a ternary operator. It has three operands.

For example, consider a statement

var = exp1 ? exp2 : exp3.

In the above statement, if exp1 is true, evaluate exp2. If exp1 is false, evaluate exp3, Variable contains result of either exp2 or exp3. The result depends on exp1.

### C Program to find Largest of Two Numbers using Conditional Operator

#include<stdio.h> #include<conio.h> main() { int a,b,max; printf("Enter two numbers : "); scanf("%d %d",&a,&b); max = a > b ? a : b; printf("Largest number : %d\n", max); getch(); }

Output:

### C Program to find Largest of Three Numbers using Conditional Operator

#include<stdio.h> #include<conio.h> main() { int a,b,c,max; printf("Enter any three numbers : "); scanf("%d %d %d", &a,&b,&c); max = a > b ?(a > c ? a : c):(b > c ? b : c); printf("Largest number = %d\n",max); getch(); }

### C Program to find Largest number without Operators

#include<conio.h> #include<stdio.h> void main() { int a, b; clrscr(); printf("\nEnter A & B Values:"); scanf("%d,%d", &a,&b); if(a>b) { printf("\nA (%d) is larger",a); } else { printf("\nB (%d) is larger",b); } getch(); }

Output:

{ 0 comments… add one now }