Write a Java Program to find the Area of the Equilateral Triangle, Perimeter, Semi Perimeter, and Altitude of the Equilateral Triangle with an example.
Java Area of an Equilateral Triangle
The Equilateral Triangle is a triangle of equal sides, with all angles equal to 60 degrees.
- If we know the side, calculate the area of an Equilateral Triangle using the formula below. Area = (√3)/4 * s² (S = Any side of the Equilateral Triangle)
- Perimeter is the distance around the edges. We can calculate the perimeter using the 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.
- We can calculate the Altitude of an Equilateral Triangle using the formula: (√3)/2 * s
Java Program to Find Area of Equilateral Triangle
This program allows the user to enter the length of any one side of an Equilateral Triangle. By using this value, this Java program will find the Area, Perimeter, Semi Perimeter, and Altitude of the Equilateral Triangle.
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); } }
Within this Java Program to find the Area of the Equilateral Triangle, the following statements will allow the User to enter the length of any side.
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 a semi perimeter using the standard formula = (3* side) / 2.
Semi = Perimeter/2;
In the next line, We are calculating the Altitude 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.
Java Program to find Area of Equilateral Triangle using Functions
In this Java program, 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.
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(" The Perimeter of a Equilateral Triangle = %.2f\n", Perimeter); System.out.format(" The Semi Perimeter of a Equilateral Triangle = %.2f\n", Semi); System.out.format(" The Altitude of a Equilateral Triangle = %.2f\n", Altitude); } }
Please Enter the Length of any Side:
9
The Area of a Equilateral Triangle = 35.07
The Perimeter of a Equilateral Triangle = 27.00
The Semi Perimeter of a Equilateral Triangle = 13.50
The Altitude of a Equilateral Triangle = 7.79
Java Program to find the Area of Equilateral Triangle using Oops
In this Area of the Equilateral Triangle example, we divide the code using Object Oriented Programming. To do this, First, we will create a Java 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(" The Perimeter of a Equilateral Triangle = %.2f\n", Perimeter); System.out.format(" The Semi Perimeter of a Equilateral Triangle = %.2f\n", Semi); System.out.format(" 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 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); } }
Please Enter the Length of any Side:
12
The Area of a Equilateral Triangle = 62.35
The Perimeter of a Equilateral Triangle = 36.00
The Semi Perimeter of a Equilateral Triangle = 18.00
The Altitude of a Equilateral Triangle = 10.39
Second Method: The Area of a Equilateral Triangle = 62.35