Skip to main content

Calculate area of equilateral triangle

The problem involves calculating the area of an equilateral triangle given the length of its side. An equilateral triangle is a type of triangle in which all three sides are of equal length and all three angles are 60 degrees. Calculating the area of an equilateral triangle is a basic geometric calculation used in various fields such as mathematics, engineering, and design.

Problem Statement

Given the length of a side of an equilateral triangle, we need to calculate and output the area of the triangle. The side length is provided as input, and we are required to use the formula for the area of an equilateral triangle to find the area.

Example

Consider an equilateral triangle with a side length of 5. This equilateral triangle can be visualized as follows:


     /\
    /  \
   /____\
    5.0

Idea to Solve

To solve this problem, we can follow these steps:

  1. Calculate the area of the equilateral triangle using the formula: area = (sqrt(3) / 4) * (side * side).
  2. Use the provided value for the side length to compute the area.

Pseudocode

function equilateral_triangle_area(side):
    area = (sqrt(3) / 4) * (side * side)
    return area

for each test case:
    side = equilateral triangle side length
    result = equilateral_triangle_area(side)
    print("Equilateral triangle [ side :", side, "]")
    print("Area :", result)

Algorithm Explanation

  1. For each test case, calculate the area of the equilateral triangle using the provided formula.
  2. Print the triangle's side length and the calculated area.

Code Solution

//C Program
//Calculate area of equilateral triangle
#include <stdio.h>
#include <math.h>

 ///Calculate area of equilateral triangle by given side
void equilateral_triangle(double side)
{
	//Calculate area of equilateral triangle
	double area = (sqrt(3) / 4) * (side * side);
	//Display the result of right angle triangle area
	printf(" Equilateral triangle  [ side : %lf ] ", side);
	printf("\n Area : %lf\n\n", area);
}
int main()
{
	//Test Cases  
	equilateral_triangle(5);
	equilateral_triangle(10);
	equilateral_triangle(7.5);
	return 0;
}

Output

 Equilateral triangle  [ side : 5.000000 ]
 Area : 10.825318

 Equilateral triangle  [ side : 10.000000 ]
 Area : 43.301270

 Equilateral triangle  [ side : 7.500000 ]
 Area : 24.356964
// Java Program
// Calculate area of equilateral triangle
class EquilateralTriangle
{
	//Calculate area of equilateral triangle by given side
	public void equilateral_triangle(double side)
	{
		//Calculate area of equilateral triangle
		double area = (Math.sqrt(3) / 4) * (side * side);
		//Display the result of right angle triangle area
		System.out.print(" Equilateral triangle [ side : " + side + " ] ");
		System.out.print("\n Area : " + area + "\n\n");
	}
	public static void main(String[] args)
	{
		EquilateralTriangle obj = new EquilateralTriangle();
		//Test Cases
		obj.equilateral_triangle(5);
		obj.equilateral_triangle(10);
		obj.equilateral_triangle(7.5);
	}
}

Output

 Equilateral triangle [ side : 5.0 ]
 Area : 10.825317547305483

 Equilateral triangle [ side : 10.0 ]
 Area : 43.30127018922193

 Equilateral triangle [ side : 7.5 ]
 Area : 24.356964481437334
// C++ Program
// Calculate area of equilateral triangle
#include<iostream>
#include <math.h>
using namespace std;
class EquilateralTriangle
{
	public:
		//Calculate area of equilateral triangle by given side
		void equilateral_triangle(double side)
		{
			//Calculate area of equilateral triangle
			double area = (sqrt(3) / 4) *(side *side);
			cout << " Equilateral triangle [ side : " << side << " ] ";
			cout << "\n Area : " << area << "\n\n";
		}
};
int main()
{
	EquilateralTriangle obj =  EquilateralTriangle();
	//Test Cases
	obj.equilateral_triangle(5);
	obj.equilateral_triangle(10);
	obj.equilateral_triangle(7.5);
	return 0;
}

Output

 Equilateral triangle [ side : 5 ]
 Area : 10.8253

 Equilateral triangle [ side : 10 ]
 Area : 43.3013

 Equilateral triangle [ side : 7.5 ]
 Area : 24.357
// C# Program
// Calculate area of equilateral triangle
using System;
class EquilateralTriangle
{
	//Calculate area of equilateral triangle by given side
	public void equilateral_triangle(double side)
	{
		//Calculate area of equilateral triangle
		double area = (Math.Sqrt(3) / 4) * (side * side);
		Console.Write(" Equilateral triangle [ side : " + side + " ] ");
		Console.Write("\n Area : " + area + "\n\n");
	}
	public static void Main(String[] args)
	{
		EquilateralTriangle obj = new EquilateralTriangle();
		//Test Cases
		obj.equilateral_triangle(5);
		obj.equilateral_triangle(10);
		obj.equilateral_triangle(7.5);
	}
}

Output

 Equilateral triangle [ side : 5 ]
 Area : 10.8253175473055

 Equilateral triangle [ side : 10 ]
 Area : 43.3012701892219

 Equilateral triangle [ side : 7.5 ]
 Area : 24.3569644814373
<?php
// Php Program
// Calculate area of equilateral triangle
class EquilateralTriangle
{
	//Calculate area of equilateral triangle by given side
	public 	function equilateral_triangle($side)
	{
		//Calculate area of equilateral triangle
		$area = (sqrt(3) / 4) *($side *$side);
		//Display the result of right angle triangle area
		echo(" Equilateral triangle [ side : ". $side ." ] ");
		echo("\n Area : ". $area ."\n\n");
	}
}

function main()
{
	$obj = new EquilateralTriangle();
	//Test Cases
	$obj->equilateral_triangle(5);
	$obj->equilateral_triangle(10);
	$obj->equilateral_triangle(7.5);
}
main();

Output

 Equilateral triangle [ side : 5 ]
 Area : 10.825317547305

 Equilateral triangle [ side : 10 ]
 Area : 43.301270189222

 Equilateral triangle [ side : 7.5 ]
 Area : 24.356964481437
// Node Js Program
// Calculate area of equilateral triangle
class EquilateralTriangle
{
	//Calculate area of equilateral triangle by given side
	equilateral_triangle(side)
	{
		//Calculate area of equilateral triangle
		var area = (Math.sqrt(3) / 4) *(side *side);
		//Display the result of right angle triangle area
		process.stdout.write(" Equilateral triangle [ side : " + side + " ] ");
		process.stdout.write("\n Area : " + area + "\n\n");
	}
}

function main(args)
{
	var obj = new EquilateralTriangle();
	//Test Cases
	obj.equilateral_triangle(5);
	obj.equilateral_triangle(10);
	obj.equilateral_triangle(7.5);
}
main();

Output

 Equilateral triangle [ side : 5 ]
 Area : 10.825317547305483

 Equilateral triangle [ side : 10 ]
 Area : 43.30127018922193

 Equilateral triangle [ side : 7.5 ]
 Area : 24.356964481437334
#  Python 3 Program
#  Calculate area of equilateral triangle
import math
class EquilateralTriangle :
	# Calculate area of equilateral triangle by given side
	def equilateral_triangle(self, side) :
		# Calculate area of equilateral triangle
		area = (math.sqrt(3) / 4) * (side * side)
		# Display the result of right angle triangle area
		print(" Equilateral triangle [ side : ", side , end = " ] ")
		print("\n Area : ", area ,"\n")
	

def main() :
	obj = EquilateralTriangle()
	# Test Cases
	obj.equilateral_triangle(5)
	obj.equilateral_triangle(10)
	obj.equilateral_triangle(7.5)


if __name__ == "__main__": main()

Output

 Equilateral triangle [ side :  5 ]
 Area :  10.825317547305483

 Equilateral triangle [ side :  10 ]
 Area :  43.30127018922193

 Equilateral triangle [ side :  7.5 ]
 Area :  24.356964481437334
#  Ruby Program
#  Calculate area of equilateral triangle
class EquilateralTriangle

	# Calculate area of equilateral triangle by given side
	def equilateral_triangle(side)
	
		# Calculate area of equilateral triangle
		area = (Math.sqrt(3) / 4) * (side * side)
		# Display the result of right angle triangle area
		print(" Equilateral triangle [ side  : ", side ," ] ")
		print("\n Area  : ", area ,"\n\n")
	end
end
def main()

	obj = EquilateralTriangle.new()
	# Test Cases
	obj.equilateral_triangle(5)
	obj.equilateral_triangle(10)
	obj.equilateral_triangle(7.5)
end
main()

Output

 Equilateral triangle [ side  : 5 ] 
 Area  : 10.825317547305483

 Equilateral triangle [ side  : 10 ] 
 Area  : 43.30127018922193

 Equilateral triangle [ side  : 7.5 ] 
 Area  : 24.356964481437334

// Scala Program
// Calculate area of equilateral triangle
class EquilateralTriangle
{
	//Calculate area of equilateral triangle by given side
	def equilateral_triangle(side: Double): Unit = {
		//Calculate area of equilateral triangle
		var area: Double = ((Math.sqrt(3) / 4)) * (side * side);
		//Display the result of right angle triangle area
		print(" Equilateral triangle [ side : " + side + " ] ");
		print("\n Area : " + area + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: EquilateralTriangle = new EquilateralTriangle();
		//Test Cases
		obj.equilateral_triangle(5);
		obj.equilateral_triangle(10);
		obj.equilateral_triangle(7.5);
	}
}

Output

 Equilateral triangle [ side : 5.0 ]
 Area : 10.825317547305483

 Equilateral triangle [ side : 10.0 ]
 Area : 43.30127018922193

 Equilateral triangle [ side : 7.5 ]
 Area : 24.356964481437334
// Swift Program
// Calculate area of equilateral triangle
import Foundation
class EquilateralTriangle
{
	//Calculate area of equilateral triangle by given side
	func equilateral_triangle(_ side: Double)
	{
		//Calculate area of equilateral triangle
		let area: Double = (sqrt(3) / 4) * (side * side);
		//Display the result of right angle triangle area
		print(" Equilateral triangle [ side : ", side ,"]");
		print(" Area : ", area ,"\n");
	}
}
func main()
{
	let obj: EquilateralTriangle = EquilateralTriangle();
	//Test Cases
	obj.equilateral_triangle(5);
	obj.equilateral_triangle(10);
	obj.equilateral_triangle(7.5);
}
main();

Output

 Equilateral triangle [ side :  5.0 ]
 Area :  10.8253175473055

 Equilateral triangle [ side :  10.0 ]
 Area :  43.3012701892219

 Equilateral triangle [ side :  7.5 ]
 Area :  24.3569644814373

Output Explanation

The provided code runs three test cases and calculates the areas of equilateral triangles with different side lengths. The output includes the triangle's side length and the corresponding calculated areas.

Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations and a square root computation, which take constant time. Therefore, the overall time complexity remains constant for each test case.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment