Area Of Triangle Program In C
Implementing the Area of Triangle in C using its coordinates:
With this approach, we will use three coordinates to indicate the locations of a triangle's three vertices on the cartesian plane.
We're going to use a really straightforward strategy. As we are aware, the formula to determine a triangle's area has the following coordinates:
They are (a1, b1), (a2, b2), and (a3, b3).
Area=1/2|a1(b2-b3)+a2(b1-b3)+a3(b1-b2)|
Simply put, we must create software to translate this logic into code.
Program:
#include <stdio.h>
#include <math.h>
int main()
{
// initialization of the coordinates present in a plane
float a1 = 1, b1 = 2;
float a2 = 3, b2 = 3;
float a3 = 5, b3 = 6;
float area_triangle= ((float)1 / 2) * abs((int) (a1 * (b2 - b3) + a2 * (b3 - b1) + a3 * (b1 - b2)));
printf("The Area of the triangle is : %f", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c1.png)
Implementing the Area of Triangle in C using its base and height:
The area of a triangle is now to be determined, and its base and height will be provided. Let's see how that works out.
If we have both, we can determine a triangle's area using its base and height.
Area=1/2 * (base) *(height)
Program:
#include <stdio.h>
#include <math.h>
int main()
{
// initialization of the base and height
float base = 4, height = 12;
//Calculating the area
float area_triangle= ((float)1/2) * base * height;
// printing the area
printf("The area of given Triangle is : %f", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c2.png)
Implementing Area for an Equilateral Triangle:
The simplest one is this one. We are aware that a triangle with equal sides is said to be equilateral. And the straightforward formula to determine its area is
Area= Sqrt(3)/4 *(side * side)
Program:
#include <stdio.h>
#include <math.h>
int main()
{
// initialization of the side for an equilateral triangle
float s = 9;
float area_triangle = (sqrt(3)/4) * (s * s);
// displaying the area
printf("The area of the given Triangle is : %f", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c3.png)
Implementing Area of Triangle in C using Heron’s Formula:
We can apply Heron's formula to get the area of practically any triangle. All three sides need to be measured directly for us to calculate them; otherwise, they should be calculable. The area of the triangle can then be determined in a couple of seconds or minutes if this occurs.
Due to the lack of a universal formula for scalene triangles, Heron's Formula is typically employed to determine the area of these triangles.
Therefore, if the height and base of a scalene triangle are uncertain, we may use Heron’s Formula.
Area=sqrt(s(s-a) * (s-b) * (s-c)) where s=(a+b+c)/2
Program:
#include <stdio.h>
#include <math.h>
int main()
{
// initialization of the sides and calculating the semi-perimeter of a triangle
float a = 4, b = 5, c = 6;
float s = (a + b + c) / 2;
//Calculate the area of the triangle
float area_triangle = sqrt(s * (s - a) * (s - b) * (s - c));
// displaying the area
printf("The area of the given triangle is : %f ", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c4.png)
Finding Area of Triangle using Functions:
Up to now, the logic has been written directly in the main function, which is fine if we only need to calculate the area once. When creating a program, if we
need to calculate the area multiple times, it is always a good idea to wrap the logic in a global function and call it each time we need to do so.
Let's use global functions to create the code for all the mentioned programs.
1. By using coordinates:
#include <stdio.h>
#include <math.h>
float area_of_triangle_by_coordinates(int a1, int b1, int a2, int b2, int a3, int b3)
{
float area_triangle= ((float)1 / 2) * abs((int) (a1 * (b2 - b3) + a2 * (b3 - b1) + a3 * (b1 - b2)));
return area_triangle;
}
int main()
{
float a1 = 1, b1 = 2;
float a2 = 3, b2 = 3;
float a3 = 5, b3 = 6;
float area_triangle=area_of_triangle_by_coordinates(a1,b1,a2,b2,a3,b3);
printf("The Area of the triangle is : %f", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c5.png)
2. By Using Base and Height:
#include <stdio.h>
#include <math.h>
float area_of_triangle_base_Height(float base,float height)
{
//Calculating the area
float area_triangle= ((float)1/2) * base * height;
}
int main()
{
// initialization of the base and height
float base = 4, height = 12;
float area_triangle=area_of_triangle_base_Height(base,height);
// printing the area
printf("The area of given Triangle is : %f", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c6.png)
3. For Equilateral Triangle:
#include <stdio.h>
#include <math.h>
float area_of_equilateral(float s)
{
float area_triangle = (sqrt(3)/4) * (s * s);
return area_triangle;
}
int main()
{
// initialization of the side for an equilateral triangle
float s = 9;
float area_triangle =area_of_equilateral(s);
// displaying the area
printf("The area of the given Triangle is : %f", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c7.png)
4. Using Heron's Formula:
#include <stdio.h>
#include <math.h>
float area_of_triangle_herons(float a,float b,float c)
{
float s = (a + b + c) / 2;
//Calculate the area of the triangle
float area_triangle = sqrt(s * (s - a) * (s - b) * (s - c));
return area_triangle;
}
int main()
{
// initialization of the sides and calculating the semi-perimeter of a triangle
float a = 4, b = 5, c = 6;
float area_triangle =area_of_triangle_herons( a, b, c);
// displaying the area
printf("The area of the given triangle is : %f ", area_triangle);
return 0;
}
OUTPUT:
![Area Of Triangle Program In C](https://static.tutorialandexample.com/c/area-of-triangle-program-in-c8.png)