Find area of triangle using base & height in java (example)
Yogesh
Given base & height of triangle, calculate the area of triangle.
Area of triangle = 0.5 * base * height.
e.g Given base = 4 and height 6.
Area of triangle = 0.5 * 4 * 6 = 12.
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