Java Program to find Area of a Triangle using Base and Height

Write a Java Program to find Area of a Triangle using base and height with an example. This Java example allows entering triangle base and height, and the product of both divides by two gives the area. Thus, the area of the triangle equals base multiplies height divided by two.

package Area;

import java.util.Scanner;

public class AreaOfTriangleBH1 {
	private static Scanner sc;

	public static void main(String[] args) {
		double tribase, triheight, triArea; 
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the Base =  ");
		tribase = sc.nextDouble();
		
		System.out.print("Please Enter the Height = ");
		triheight = sc.nextDouble();

		triArea = (tribase * triheight) / 2;

		System.out.format("Triangle Area using Base & Height = %.2f",triArea);
	}
}
Please Enter the Base =  18
Please Enter the Height = 25
Triangle Area using Base & Height = 225.00

In this Java program, we declared a triangleArea function that returns the triangle area.

package Area;

import java.util.Scanner;

public class AreaOfTriangleBH2 {
	private static Scanner sc;

	public static void main(String[] args) {
		float tribase, triheight, triArea; 
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the Triangle Base =  ");
		tribase = sc.nextFloat();
		
		System.out.print("Please Enter the Triangle Height = ");
		triheight = sc.nextFloat();

		triArea = triangleArea(tribase, triheight);

		System.out.format("Triangle Area using Base & Height = %.2f",triArea);
	}
	
	public static float triangleArea(float tribase, float triheight) {
		return (tribase * triheight) / 2;
	}
}
Java Program to find Area of a Triangle using Base and Height