Saturday, January 30, 2010

CHAP 2[F]f Checking whether triangle is valid

If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater than the largest of the three sides.

void main()
{
float a,b,c,biggest;
printf("\nEnter the length of 3 sides of the triangle");
scanf("%f%f%f",&a,&b,&c);

biggest=(a>b?(a>c?a:c):(b>c?b:c));

if (biggest==a)
{
if(b+c>a)
printf("\nThe triangle is valid");
else
printf("\nThe triangle is invalid");
}

else if (biggest==b)
{
if(a+c>b)
printf("\nThe triangle is valid");
else
printf("\nThe triangle is invalid");
}

else
{
if(a+b>c)
printf("\nThe triangle is valid");
else
printf("\nThe triangle is invalid");
}
}

6 comments: