Algorithm to calculate area of a triangle.

1. Start.

2. Read the three sides of triangle ( let a, b, c ).

3. If (a>0 && b>0 && c>0 && a+b>c && b+c>a && a+c>b)
         Triangle can be formed ; Go to Step 5.

4. Else Triangle cannot be formed ; Go to Step 9.

5. If a == b == c , 
     Triangle is Equilateral
      Area = square_root(3)/2*a*a;

6. Else if ( a+b == c )
      Triangle is right angled a & b are base and height
       Area = 0.5*a*b;
    Else if ( b+c == a )
       Triangle is right angled b & c are base and height
       Area = 0.5*b*c;
    Else if ( a+c == a )
      Triangle is right angled a & c are base and height
       Area = 0.5*a*c;
7. Else
     s = (a+b+c)/2.0;
     Area = square_root_of(s*(s-a)*(s-b)*(s-c));

8. Print Area.

9. End.

/* Formula used in Step 7 is valid for all valid triangle.
Only writing Step 7 formula is also a correct algorithm to calculate area of any valid triangle  */

No comments:

Post a Comment