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)
Program to calculate Area of an Equilateral triangle
In this program, user is asked to enter the side of the triangle. All sides of this triangle are equal and we just need to use this value in the above formula to calculate the area.
Here we will also need to include the math.
h header file as we are using the function sqrt()
which is predefined in the math.h header file.
The steps followed in this program are:
1. Prompt user to input the side of the triangle. Store this entered value in variable side
. We need this variable in next steps to calculate area.
2. Use the the above mentioned formula to calculate area based on the input value of side. = (sqrt(3) / 4) * (side * side).
3. Print the area of equilateral triangle as the output.
#include <stdio.h>
#include <math.h> //to use sqrt() function
int main()
{
// Variable area to store calculated area, "temp"
// variable to store the result of temporary calculation
// and side represents the side of the triangle
float side, area, temp;
// Prompt user to input side of the Equilateral triangle
printf("Enter the Side of the triangle: ");
scanf("%f",&side);
//Calculate and print the area of Equilateral Triangle
temp = sqrt(3) / 4 ;
area = temp * side * side ;
printf("Area of Equilateral Triangle is: %f",area);
return 0;
}
Output:
Leave a Reply