Thursday, September 20, 2012

second largest among 3 numbers

#include<stdio.h>
#include<conio.h>
void main();
{
int lar2,a,b,c;
clrscr();
printf("Enter the values\n");
scanf("%d%d%d",&a,&b,&c);
lar2= ( a>b?(a>c?c:a):(b>c?c:b));
printf("%d",lar2);
getch();
}

The program may seems to be difficult.But this program is so simple that we only need to know about the conditional operators.
a small explanation about conditional operators

The Conditional Operator in C Language

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.

Syntax:
condition ? result1 : result2

If the condition is true, result1 is returned else result2 is returned.

Examples:

10==5 ? 11: 12 // returns 12, since 10 not equal to 5.
10!=5 ? 4 : 3 // returns 4, since 10 not equal to 5.
12>8 ? a : b // returns the value of a, since 12 is greater than 8.

No comments:

Post a Comment