Find Greatest Number
Find Greatest Number from three numbers Using Ternary Operator
Ternary operator
If any operator is used on three operands or variable is known as ternary operator. It can be represented with " ?: "
In the above symbol exp1 is condition and exp2 and exp3 will be either value or variable or statement or any mathematical expression. If condition will be true exp2 will be executer otherwise exp3 will be executed.
Example 1:
int a =7, b=20, c; C=a>b;a:b; Value of (c) -----> 20 Example 2: C=a< b ? a + b : a - b Value of (c) -----> 27
Find largest number among 3 numbers
int a=15, b=21, c=-13, Large; Large=(a>b)?(a>c?a:c):(b>c?b:c) Value of (Large) -----> 21
/***************************************************** Find Greatest Number ******************************************************/ #include< stdio.h> #include< conio.h> void main() { int a,b,c,largest; clrscr(); printf("Enter any Three Numbers: "); scanf("%d%d%d",&a,&b,&c); largest=(a>b)?(a>c?a:c):(b>c?b:c); printf("Largest Values: %d",largest); getch(); }
Output
No comments:
Post a Comment