Area of Triangle

Calculate the area of a triangle using base and height.

BeginnerTopic: Module 1: Basic Java Programs
Back

Java Area of Triangle Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter base: ");
        double base = sc.nextDouble();
        System.out.print("Enter height: ");
        double height = sc.nextDouble();

        double area = 0.5 * base * height;
        System.out.println("Area of triangle = " + area);

        sc.close();
    }
}
Output
Enter base: 10
Enter height: 5
Area of triangle = 25.0

Understanding Area of Triangle

We use the formula area = 1/2 × base × height.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents