C Program to Find the Area and Perimeter of an Equilateral Triangle

C Program to Find the Area and Perimeter of an Equilateral Triangle

C Program to Find the Area and Perimeter of an Equilateral Triangle


Introduction

An equilateral triangle is a special type of triangle where all three sides are equal, and all three angles are 60 degrees. It has unique properties that make its area and perimeter easy to calculate using straightforward formulas.

In this tutorial, we will learn how to calculate the area and perimeter of an equilateral triangle using a C program. We will explore the mathematical formulas, write a C program, and explain the output in detail.


Formula

The formulas to calculate the area and perimeter of an equilateral triangle are as follows:

  • Perimeter: Since all sides are equal, the perimeter is given by:
    Perimeter = 3 × side
  • Area: The area of an equilateral triangle is calculated using the formula:
    Area = (√3 / 4) × side²
    Here, √3 is the square root of 3, which is approximately 1.732.

These formulas allow us to calculate the required properties quickly when the length of a side is known.


C Code

The following is the C program to calculate the area and perimeter of an equilateral triangle:

#include <stdio.h>
#include <math.h>

int main() {
    float side, area, perimeter;

    // Input from the user
    printf("Enter the length of a side of the equilateral triangle: ");
    scanf("%f", &side);

    // Calculate perimeter
    perimeter = 3 * side;

    // Calculate area
    area = (sqrt(3) / 4) * (side * side);

    // Display results
    printf("Perimeter of the equilateral triangle: %.2f\n", perimeter);
    printf("Area of the equilateral triangle: %.2f\n", area);

    return 0;
}
        

Output

When you execute the above program, you will be prompted to input the length of a side. The program will then calculate and display the area and perimeter. Below is a sample run:

Enter the length of a side of the equilateral triangle: 5
Perimeter of the equilateral triangle: 15.00
Area of the equilateral triangle: 10.83
        

Here, the side length was entered as 5. The program computed the perimeter as 15 (3 × 5) and the area as approximately 10.83.


Explanation

The program works in the following steps:

  • Input: The program asks the user to enter the length of a side of the equilateral triangle. This value is stored in the variable side.
  • Perimeter Calculation: Using the formula 3 × side, the perimeter is calculated and stored in the variable perimeter.
  • Area Calculation: The formula (√3 / 4) × side² is used to calculate the area. The sqrt function from math.h is used to compute the square root of 3.
  • Output: Finally, the program displays the calculated perimeter and area using formatted output with two decimal points for clarity.

Key Points

  • An equilateral triangle is defined by having all sides of equal length and angles of 60°.
  • The program uses the math.h library for the sqrt function to calculate the square root of 3.
  • The formula for the area is derived from basic geometry principles and provides an accurate result when the side length is known.
  • The perimeter is simply the sum of the lengths of all sides.