C Program To Calculate Area Of Equilatral Triangle

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: 5Area of the equilateral triangle: 10.83

Output 2:

Enter the side length of the equilateral triangle: 25Area of the equilateral triangle: 270.51

Explanation:

  1. Include Standard Libraries:
    • #include <stdio.h> for standard input and output functions such as printf and scanf.
    • #include <math.h> The sqrt function which we have used in the program is present in this library.
  2. 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.
  3. 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 the side variable.
  4. Calculate Area:
    • area = (sqrt(3) / 4) * (side * side); This formula is used for calculating the area.
  5. Print Result:
    • printf("Area of the equilateral triangle: %.2f\n", area); prints the area of equilateral triangle with two decimal places.
  6. Return Statement:
    • return 0; indicates that the program executed successfully.
❮ C TutorialC Programs ❯

Top Related Articles:

  1. C Program to print cube of a number upto an integer
  2. C Program to concatenate two strings without using strcat
  3. C Program to read and print employee details using structure
  4. C Program to Print Right Triangle Star Pattern
  5. C Program to find greatest of three numbers

Tag » Area Of Equilateral Triangle C Program