Saturday, October 5, 2013

Determining the Type of a Triangle

Consider a triangle with three sides measuring a, b, and c units. A triangle is a right-angled triangle if a2 + b2 = c2 Allow a tolerance of 0.000001 in the comparison in the above case i.e. a2 + b2 = c2 +/- 0.000001
A triangle is an isosceles triangle if any two of its sides are equal. A triangle is an equilateral triangle if all the three sides are equal. Three values can be the dimensions of a triangle if and only if the sum of every pair of values is greater than the third value. Otherwise, it is an invalid triangle.

 
Write a program that reads three real numbers, finds out whether they can be the sides of the triangle, and if they do, prints what type of triangle it is. Even though all equilateral triangles are isosceles, your program should classify an equilateral triangle to be an equilateral only. Similarly, isosceles right-angled triangles should be classified as right-angled and not isosceles. A valid triangle, which does not belong to any of the special types, belongs to the notspecial category.

 
The three sides will be given as real numbers separated by blanks. The program should print the type of the triangle in words using lower case letters followed by a newline.

 
Thus the possible answers are: 

  • invalid 
  • right-angled 
  • isosceles 
  • equilateral 
  • notspecial

Solution:

#include<stdio.h>
#include<math.h>

int main(void)
{
    float a,b,c;
    scanf("%f %f %f",&a,&b,&c);
    if((a+b>c && b+c>a && c+a>b) && (a>0 && b>0 && c>0))
    {
        if(a==b && b==c)
        printf("equilateral\n");
   
     else if( sqrt(pow((a*a+b*b-c*c),2))<0.000001 || 
sqrt(pow((b*b+c*c-a*a),2))<0.000001 || 
sqrt(pow((c*c+a*a-b*b),2))<0.000001)

        printf("right-angled\n");
        else if((a==b)||(b==c)||(c==a))
        printf("isosceles\n");
        else
        printf("notspecial\n");
    }
    else
    printf("invalid\n");

return 0;
}

No comments:

Post a Comment