Program to Calculate Area and Perimeter of Equilateral Triangle

Python Program to Calculate Area and Perimeter of Equilateral Triangle

In the previous article, we have discussed Python Program for Maximum Number of 2×2 Squares That Can be Fit Inside a Right Isosceles Triangle
Given the side length of the equilateral triangle and the task is to calculate the area and perimeter of the equilateral triangle with the given side length.

Equilateral triangle:

An equilateral triangle is one with equal sides and angles on all three sides. The internal angles of an equilateral triangle are all 60 degrees.

Formulas:

Let ‘a’ be the side length of the equilateral triangle.

Area = (sqrt(3)/4) * a * a

Perimeter = 3*a

Examples:

Example1:

Input:

Given side length = 8

Output:

The Area of Equilateral Triangle with the given side length [ 8 ] =  27.712813
The Perimeter of Equilateral Triangle with the given side length [ 8 ] =  24.000000

Example2:

Input:

Given side length = 5

Output:

The Area of Equilateral Triangle with the given side length [ 5 ] =  10.825318
The Perimeter of Equilateral Triangle with the given side length [ 5 ] =  15.000000

Program to Calculate Area and Perimeter of Equilateral Triangle in Python

Below are the ways to calculate the area and perimeter of the equilateral triangle with the given side length:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import the math module using the import keyword.
  • Give the side length as static input and store it in a variable.
  • Calculate the area of the equilateral triangle using the above mathematical formula given and store it in another variable.
  • Calculate the perimeter of the equilateral triangle using the above mathematical formula given and store it in another variable.
  • Print the area of the equilateral triangle for the given side length.
  • Print the perimeter of the equilateral triangle for the given side length.
  • The Exit of the Program.

Below is the implementation:

# Import the math module using the import keyword.
from math import *
# Give the side length as static input and store it in a variable.
gvn_side_len = 8
# Calculate the area of the equilateral triangle using the above mathematical formula
# given and store it in another variable.
rslt_area = (sqrt(3) / 4) * gvn_side_len * gvn_side_len
# Calculate the perimeter of the equilateral triangle using the above mathematical
# formula given and store it in another variable.
rslt_perimetr = 3 * gvn_side_len
# Print the area of the equilateral triangle for the given side length.
print(
    "The Area of Equilateral Triangle with the given side length [", gvn_side_len, "] = % f" % rslt_area)
# Print the perimeter of the equilateral triangle for the given side length.
print(
    "The Perimeter of Equilateral Triangle with the given side length [", gvn_side_len, "] = % f" % rslt_perimetr)

Output:

The Area of Equilateral Triangle with the given side length [ 8 ] =  27.712813
The Perimeter of Equilateral Triangle with the given side length [ 8 ] =  24.000000

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import the math module using the import keyword.
  • Give the side length as user input using the float(input()) function and store it in a variable.
  • Calculate the area of the equilateral triangle using the above mathematical formula given and store it in another variable.
  • Calculate the perimeter of the equilateral triangle using the above mathematical formula given and store it in another variable.
  • Print the area of the equilateral triangle for the given side length.
  • Print the perimeter of the equilateral triangle for the given side length.
  • The Exit of the Program.

Below is the implementation:

# Import the math module using the import keyword.
from math import *
# Give the side length as user input using the float(input()) function and
# store it in a variable.
gvn_side_len = float(input("Enter some random number = "))
# Calculate the area of the equilateral triangle using the above mathematical formula
# given and store it in another variable.
rslt_area = (sqrt(3) / 4) * gvn_side_len * gvn_side_len
# Calculate the perimeter of the equilateral triangle using the above mathematical
# formula given and store it in another variable.
rslt_perimetr = 3 * gvn_side_len
# Print the area of the equilateral triangle for the given side length.
print(
    "The Area of Equilateral Triangle with the given side length [", gvn_side_len, "] = % f" % rslt_area)
# Print the perimeter of the equilateral triangle for the given side length.
print(
    "The Perimeter of Equilateral Triangle with the given side length [", gvn_side_len, "] = % f" % rslt_perimetr)

Output:

Enter some random number = 5
The Area of Equilateral Triangle with the given side length [ 5.0 ] = 10.825318
The Perimeter of Equilateral Triangle with the given side length [ 5.0 ] = 15.000000

If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.