Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to calculate area and perimeter of equilateral triangle
An equilateral triangle is a closed figure with three equal sides. We can calculate its area and perimeter using mathematical formulas that involve the side length.
Syntax
Area = (√3/4) * side<sup>2</sup> Perimeter = 3 * side
Formulas
For an equilateral triangle with side length a −
- Area of equilateral triangle = (√3)/4 * a2
- Perimeter of equilateral triangle = 3 * a
Logic of Code
To find the area of an equilateral triangle, the program uses sqrt() function from the math library to calculate the square root of 3. The math.h header provides mathematical functions required for the calculation.
Example
The following code demonstrates how to calculate the area and perimeter of an equilateral triangle ?
#include <stdio.h>
#include <math.h>
int main() {
int side = 5, perimeter;
float area;
// Calculate perimeter
perimeter = 3 * side;
// Calculate area using formula: (sqrt(3)/4) * side^2
area = (sqrt(3) / 4) * (side * side);
printf("Side of equilateral triangle: %d<br>", side);
printf("Perimeter is: %d<br>", perimeter);
printf("Area is: %.2f<br>", area);
return 0;
}
Side of equilateral triangle: 5 Perimeter is: 15 Area is: 10.83
Key Points
- The
sqrt()function requires linking with the math library using-lmflag during compilation. - Area calculation involves floating−point arithmetic, so we use
floatdata type. - Perimeter involves simple multiplication, so
intis sufficient.
Conclusion
Calculating the area and perimeter of an equilateral triangle in C is straightforward using basic mathematical formulas. The program efficiently uses the math library's sqrt() function for accurate area calculation.
