# Determining the Area of Different Shaped Triangles in C

In this C programming algorithm tutorial we are looking at how to implement the mathematical formulas to determine area of different shaped triangles. We will look at the triangle, right angled triangle and equilateral triangle. We will give you the formula, the C source code and the results.

## Area of a Right Angled Triangle

The mathematical formula to determine the area of a right angled triangle is:

area = (1/2) * height * width

Take a look at the C language implementation of the formula:

``````
#include <stdio.h>

int main() {
float height, width;
float area;

printf("Enter height and width of the right angled triangle: ");
scanf("%f%f",&height,&width);

area = 0.5 * height * width;
printf("Area of right angled triangle is: %.3f\n", area);

return 0;
}

``````

Output example of the program:

``````
Enter height and width of the right angled triangle: 2 1
Area of right angled triangle is: 1.000

``````

## Area of any Triangle

The mathematical formula to determine the area of any triangle (when all sides are known) is:

Area = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c)/2

Take a look at the C language implementation of the formula:

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

int main() {
float a,b,c;
float s,area;

printf("Enter size of each sides of the triangle:");
scanf("%f%f%f",&a,&b,&c);

s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));

printf("Area of triangle is: %.3f\n",area);
return 0;
}

``````

Note: if you compile with gcc, don’t forget to add the option –lm to link the math library.

The result of the program:

``````
Enter size of each sides of the triangle:5 12 13
Area of triangle is: 30.000

``````

## Area of an Equilateral Triangle

The mathematical formula to determine the area of an equilateral triangle (a triangle where the three sides are equal) is:

area = (√3)/4 * a2

Take a look at the C language implementation of the formula:

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

int main() {
float a;
float area;

printf("Enter size of a side of the equilateral triangle: ");
scanf("%f",&a);

area = sqrt(double(3))/4*(a*a);

printf("Area of equilateral triangle is: %.2f\n", area);
return 0;
}
``````

Note: if you compile with gcc, don’t forget to add the option –lm to link the math library.

The result of the program will look something like this:

``````
Enter size of a side of the equilateral triangle: 4
Area of equilateral triangle is: 6.93

``````

As you can see, these formulas are easy to implement. Also take a look at how to implement formulas to determine the area of a Rectangle, Circle and Trapezium.

That’s all for this tutorial.

This entry was posted in Programming Algorithms. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! or use to share this post with others.