In this tutorial, you will learn how to write a C program to calculate area of an equilateral triangle. A triangle is called equilateral triangle if all three of its sides are equal.
Formula to calculate area of an equilateral triangle:
Area = (sqrt(3)/4 )* (side * side)
C Program to calculate Area of an Equilateral triangle
The explanation of the program is available at the end of this program.
#include <stdio.h>
#include <math.h>
int main() {
float side, area;
// Prompt user for the side length
printf("Enter the side length of the equilateral triangle: ");
scanf("%f", &side);
// Calculate the area of equilateral triangle
area = (sqrt(3) / 4) * (side * side);
// print the area value after calculation
printf("Area of the equilateral triangle: %.2f\n", area);
return 0;
}
Output:
Output 1:
Enter the side length of the equilateral triangle: 5
Area of the equilateral triangle: 10.83
Output 2:
Enter the side length of the equilateral triangle: 25
Area of the equilateral triangle: 270.51
Explanation:
- Include Standard Libraries:
#include <stdio.h>
for standard input and output functions such asprintf
andscanf
.#include <math.h>
Thesqrt
function which we have used in the program is present in this library.
- Declare Variables:
float side, area;
The side variable is to hold the value of side of the equilateral triangle and area variable is to hold the area value after calculation.
- User Input:
printf("Enter the side length of the equilateral triangle: ");
prompts the user to enter the length of side of triangle.scanf("%f", &side);
reads the value entered by user and store it into theside
variable.
- Calculate Area:
area = (sqrt(3) / 4) * (side * side);
This formula is used for calculating the area.
- Print Result:
printf("Area of the equilateral triangle: %.2f\n", area);
prints the area of equilateral triangle with two decimal places.
- Return Statement:
return 0;
indicates that the program executed successfully.
Leave a Reply