Write a Java Program to find Area of Equilateral Triangle, Perimeter, Semi Perimeter, and Altitude of Equilateral Triangle with example.

## Java Area of an Equilateral Triangle

The Equilateral Triangle is a triangle of equal sides and all angles are equal to 60 degrees.

- If we know the side, then calculate the area of an Equilateral Triangle using the below formula. Area = (√3)/4 * s² (S = Any side of the Equilateral Triangle)
- Perimeter is the distance around the edges. We can calculate the perimeter using math formula: Perimeter = 3s
- We can calculate Semi Perimeter of an Equilateral Triangle using the formula: 3s/2. Or we can say Perimeter/2. Here s is the side of an Equilateral Triangle
- We can calculate Altitude of an Equilateral Triangle using the formula: (√3)/2 * s

## Java Program to find Area of Equilateral Triangle

This Java program allows the user to enter the length of any one side of an Equilateral Triangle. By using this value, this Java program will calculate the Area, Perimeter, Semi Perimeter, Altitude of the Equilateral Triangle.

// Java Program to find Area of Equilateral Triangle package Area; import java.util.Scanner; public class AreaOfEquilateralTriangle { private static Scanner sc; public static void main(String[] args) { double side, Area, Perimeter, Semi, Altitude; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any Side: "); side = sc.nextDouble(); Area = (Math.sqrt(3)/4)*(side * side); Perimeter = 3 * side; Semi = Perimeter/2; Altitude = (Math.sqrt(3)/2) * side; System.out.format("\n The Area of a Equilateral Triangle = %.2f\n",Area); System.out.format("\n The Perimeter of a Equilateral Triangle = %.2f\n", Perimeter); System.out.format("\n The Semi Perimeter of a Equilateral Triangle = %.2f\n", Semi); System.out.format("\n The Altitude of a Equilateral Triangle = %.2f\n", Altitude); } }

**OUTPUT**

**ANALYSIS**

Within this Java Program to find Area of Equilateral Triangle, the following statements will allow the User to enter the length of any side in the Equilateral Triangle.

System.out.println("\n Please Enter the Length of any Side: "); side = sc.nextDouble();

Next line, we are using the Mathematical Formula to calculate the Area of an Equilateral Triangle. Here Math.sqrt() is the math function, which is to calculate the square root.

Area = (Math.sqrt(3)/4)*(side * side);

In the next line, We are calculating the Perimeter of an Equilateral Triangle using the formula

Perimeter = 3 * side;

Within the next line, We are calculating the semi perimeter of an Equilateral Triangle using the following formula. We can also find semi perimeter using the standard formula = (3* side) / 2.

Semi = Perimeter/2;

In the next line, We are calculating the Altitude of an Equilateral Triangle using the formula:

Altitude = (Math.sqrt(3)/2) * side;

The following System.out.format statements will help us to print the Perimeter, Semi Perimeter, Altitude and Area of an Equilateral Triangle

System.out.format("\n The Area of a Equilateral Triangle = %.2f\n",Area); System.out.format("\n The Perimeter of a Equilateral Triangle = %.2f\n", Perimeter); System.out.format("\n The Semi Perimeter of a Equilateral Triangle = %.2f\n", Semi); System.out.format("\n The Altitude of a Equilateral Triangle = %.2f\n", Altitude);

## Java Program to find Area of Equilateral Triangle using Functions

In this Java program to calculate the Area of an Equilateral triangle, we are going to use the logic that we specified in the first example. But we will separate the logic and place it in a method.

// Java Program to find Area of Equilateral Triangle using Functions package Area; import java.util.Scanner; public class AreaOfEquilateralTriangleUsingMethods { private static Scanner sc; public static void main(String[] args) { double side; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any Side: "); side = sc.nextDouble(); AreaofRightTriangle(side); } public static void AreaofRightTriangle( double side ) { double Area, Perimeter, Semi, Altitude; Area = (Math.sqrt(3)/4)*(side * side); Perimeter = 3 * side; Semi = Perimeter/2; Altitude = (Math.sqrt(3)/2) * side; System.out.format("\n The Area of a Equilateral Triangle = %.2f\n",Area); System.out.format("\n The Perimeter of a Equilateral Triangle = %.2f\n", Perimeter); System.out.format("\n The Semi Perimeter of a Equilateral Triangle = %.2f\n", Semi); System.out.format("\n The Altitude of a Equilateral Triangle = %.2f\n", Altitude); } }

**OUTPUT**

## Java Program to find Area of Equilateral Triangle using Oops

In this Java Area of Equilateral Triangle example, we are dividing the code using the Object Oriented Programming. To do this, First, we will create a class that holds methods.

package Area; public class AreaOfaEquilateralTriangle { public void AreaofEquilateralTriangle( double side ) { double Area, Perimeter, Semi, Altitude; Area = (Math.sqrt(3)/4)*(side * side); Perimeter = 3 * side; Semi = Perimeter/2; Altitude = (Math.sqrt(3)/2) * side; System.out.format("\n The Area of a Equilateral Triangle = %.2f\n",Area); System.out.format("\n The Perimeter of a Equilateral Triangle = %.2f\n", Perimeter); System.out.format("\n The Semi Perimeter of a Equilateral Triangle = %.2f\n", Semi); System.out.format("\n The Altitude of a Equilateral Triangle = %.2f\n", Altitude); } public double EquilateralTriangle( double side ) { double Area; Area = (Math.sqrt(3)/4)*(side * side); return Area; } }

Within the Main Java Area of Equilateral Triangle class, we create an instance of the above-specified class and call the methods.

package Area; import java.util.Scanner; public class AreaOfEquilateralTriangleUsingClass { private static Scanner sc; public static void main(String[] args) { double side, Area; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any Side: "); side = sc.nextDouble(); AreaOfaEquilateralTriangle aet = new AreaOfaEquilateralTriangle(); aet.AreaofEquilateralTriangle(side); Area = aet.EquilateralTriangle(side); System.out.format("\n Second Method: The Area of a Equilateral Triangle = %.2f", Area); } }

**OUTPUT**