Site icon

Find area of triangle using base & height in java (example)

Fig 1: Area of triangle

Program: calculate area of triangle using height & base in java (example)

package org.learn;

import java.util.Scanner;

public class AreaOfTriangle {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.printf("1. Enter base of triangle : ");
			double base = scanner.nextDouble();		
			
			System.out.printf("2. Enter height of triangle : ");
			double height = scanner.nextDouble();			
				
			//Area of triangle is 0.5 * (base * height) 
			double area = 0.5 * base * height;
			//Print area up to two precision
			System.out.printf("3. Area of triangle is : %4.2f",area);
		}
	}
}

Output: find area of triangle using base & height in java (example):

1. Enter base of triangle : 6
2. Enter height of triangle : 12
3. Area of triangle is : 36.00

1. Enter base of triangle : 10
2. Enter height of triangle : 20
3. Area of triangle is : 100.00
Exit mobile version