# Calculating the Area of Triangle in all 4 ways in Java

Hey coders! In this tutorial, we will learn the different ways of calculating the area of a Triangle and implement it in code in Java.

So a Triangle has three sides and three angles. Depending on the parameters given the area of a triangle can be calculated in four different methods. We shall look into these four methods in detail.

### When the base and height are given: Area of a triangle in Java

This is the simplest and the most common method for calculating the area of a Triangle.

We use this method when the only known parameters are the base and height of a Triangle.

The area of Triangle is given by:

**Area = (0.5) * (Base) * (Height)**

Below is the .java file:

import java.util.*; class Triangle { public static void main(String[]args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the base: "); int base = scan.nextInt(); System.out.print("Enter the height: "); int height = scan.nextInt(); double area = (0.5) * (base * height); System.out.println("The area of triangle is: "+area); } }

#### Output:

Enter the base: 30 Enter the height: 21 The area of triangle is: 315.0

### Area of a triangle in Java When Three sides are given:

When the three sides of a triangle are given we can calculate the area by using the following formulas:

**s=(0.5)*(a+b+c)**

where s = Semiperimeter of the triangle,

a = Length of side BC,

b = Length of side CA,

c = Length of side AB.

**Area=sqrt (s * (s – a) * (**s – b**) * (**s – c**))**

Below is the .java file:

import java.util.*; import java.lang.Math; class triangle2 { public static void main(String[]args) { Scanner scan = new Scanner(System.in); System.out.print("Enter Side 1: "); int a = scan.nextInt(); System.out.print("Enter Side 2: "); int b = scan.nextInt(); System.out.print("Enter Side 3: "); int c = scan.nextInt(); double s = (0.5) * (a + b + c); double area = (double)Math.sqrt(s * (s - a) * (s - b) * (s - c)); System.out.println("The area of triangle is: "+area); } }

#### Output:

Enter Side 1: 5 Enter Side 2: 9 Enter Side 3: 7 The area of triangle is: 17.41228014936585

### Area of a triangle in Java When the triangle is Equilateral

When the given triangle is an equilateral triangle the area of the triangle can be found by using the below formula:

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

Where s = a side of the triangle

Below is the .java file:

import java.util.*; import java.lang.Math; class triangle3 { public static void main(String[]args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the side: "); int s = scan.nextInt(); double area = (double)(s * s) * ((Math.sqrt(3)) / 4); System.out.println("The area of triangle is: "+area); } }

#### Output:

Enter the side: 9 The area of triangle is: 35.074028853269766

### Method 4: Calculating the Area of Triangle using Trignometry

When two sides and one angle is given, we can use the trigonometric method of calculating the area of the Triangle.

The formula is given by:

**Area = (((side 1) * (side 2)) / 2) * Sin(angle)**

**Note**: The angle must be in Radians and not in Degrees.

Hence we use the java.lang.Math package and use the necessary functions to calculate the area of the Triangle.

Below is the .java file:

import java.util.*; import java.lang.Math; class triangle4 { public static void main(String[]mari) { Scanner scan = new Scanner(System.in); System.out.print("Enter Side 1: "); int s1 = scan.nextInt(); System.out.print("Enter Side 2: "); int s2 = scan.nextInt(); System.out.print("Enter Angle: "); int degrees = scan.nextInt(); double radians = Math.toRadians(degrees); double area=(double)((s1*s2)/2)*(Math.sin(radians)); System.out.println("The area of triangle is: "+area); } }

#### Output:

Enter Side 1: 25 Enter Side 2: 30 Enter Angle: 60 The area of triangle is: 324.7595264191645

Hope you understood the Solutions ðŸ™‚

Any questions please feel free to drop in your comments.

You can also check:

- Time conversion in Java
- Lucas Series in Java
- Pangram Checking in Java
- Sum of all numbers in a String in Java

## Leave a Reply