Area of a triangle java – Java Program to Find Area of Isosceles Triangle

Area of a triangle java: In the previous article we have discussed Java Program to Find Volume and Surface Area of Cube

In this article we will discuss about how to find area of Isosceles triangle.

Program to Find Area of Isosceles Triangle

Before jumping into the program directly, let’s first see how we calculate area of Isosceles triangle.

Formula for Altitude of Isosceles Triangle: (sqrt(pow(s1, 2) – (pow(s2, 2) / 4)))

Formula for Area of Isosceles Triangle: (1 * s2 * h) / 2;

Where,

  • s1‘ represents length of the equal sides of an isosceles triangle.(first and second side)
  • s2‘ represents length of the unequal sides of an isosceles triangle.(third side/base of isosceles triangle)
  • pow is the power means (s1,2) represents square of s1.
  • sqrt is the square root.

Let’s see different ways to do it.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Method-1: Java Program to Find Area of Isosceles Triangle By Using Static Value

In this approach the length of equal sides of isosceles triangle and the length of base is already declared in the program, which will be used to calculate the area based on the area calculation formula. So, let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    // finding the altitude
    static float altitude(float s1, float s2)
    {
        // returning altitude
        return (float)(Math.sqrt(Math.pow(s1, 2) - (Math.pow(s2, 2) / 4)));
    }
 
    // finding area
    static float area(float s2, float h)
    {
 
        // returning area
        return (1 * s2 * h) / 2;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        float s1 = 2, s2 = 3;
        float h = altitude(s1, s2);
        System.out.println("Altitude= " + h );
        float a = area(s2, h);
        System.out.print("Area= " + a );
    }
}
Output:

Altitude Isosceles triangle = 1.3228756
Area of Isosceles triangle = 1.9843135

Method-2: Java Program to Find Area of Isosceles Triangle By Using User Input Value

In this approach the length of equal sides of isosceles triangle and the length of base will be taken as input from the user, which will be used to calculate the area based on the area calculation formula. So, let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    // finding the altitude
    static float altitude(float s1, float s2)
    {
        // returning altitude
        return (float)(Math.sqrt(Math.pow(s1, 2) - (Math.pow(s2, 2) / 4)));
    }
 
    // finding area
    static float area(float s2, float h)
    {
 
        // returning area
        return (1 * s2 * h) / 2;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the length of equal sides = " );
        float s1 = sc.nextFloat(); 
        System.out.println("Enter the length of base(unequal side) = " );
        float s2 = sc.nextFloat();
        float h = altitude(s1, s2);
        System.out.println("Altitude Isosceles triangle = " + h );
        float a = area(s2, h);
        System.out.print("Area of Isosceles triangle = " + a );
    }
}
Output:

Enter the length of equal sides = 5
Enter the length of base(unequal side) = 3
Altitude Isosceles triangle = 4.769696
Area of Isosceles triangle = 7.1545444

Our website provided core java programs examples with output aid beginners and expert coders to
test their knowledge gap and learn accordingly.

Related Java Programs: