0
0
JavaProgramBeginner · 2 min read

Java Program to Calculate Area of Triangle

You can calculate the area of a triangle in Java using the formula area = 0.5 * base * height. For example: double area = 0.5 * base * height;.
📋

Examples

Inputbase = 4, height = 3
OutputArea of triangle is 6.0
Inputbase = 10, height = 5
OutputArea of triangle is 25.0
Inputbase = 0, height = 7
OutputArea of triangle is 0.0
🧠

How to Think About It

To find the area of a triangle, you need to know its base and height. The area is half the product of the base and height, so multiply base and height, then divide by 2.
📐

Algorithm

1
Get the base of the triangle from the user
2
Get the height of the triangle from the user
3
Calculate area by multiplying base and height, then dividing by 2
4
Display the calculated area
💻

Code

java
import java.util.Scanner;

public class TriangleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter base of the triangle: ");
        double base = scanner.nextDouble();
        System.out.print("Enter height of the triangle: ");
        double height = scanner.nextDouble();
        double area = 0.5 * base * height;
        System.out.println("Area of triangle is " + area);
        scanner.close();
    }
}
Output
Enter base of the triangle: 4 Enter height of the triangle: 3 Area of triangle is 6.0
🔍

Dry Run

Let's trace the example where base = 4 and height = 3 through the code

1

Input base

User enters base = 4

2

Input height

User enters height = 3

3

Calculate area

area = 0.5 * 4 * 3 = 6.0

4

Print result

Output: Area of triangle is 6.0

StepVariableValue
1base4
2height3
3area6.0
💡

Why This Works

Step 1: Read inputs

The program reads the base and height values from the user using Scanner.

Step 2: Calculate area

It calculates the area using the formula 0.5 * base * height, which gives the correct area of a triangle.

Step 3: Display output

Finally, it prints the calculated area to the screen for the user to see.

🔄

Alternative Approaches

Using Heron's formula
java
import java.util.Scanner;

public class TriangleAreaHeron {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter side a: ");
        double a = scanner.nextDouble();
        System.out.print("Enter side b: ");
        double b = scanner.nextDouble();
        System.out.print("Enter side c: ");
        double c = scanner.nextDouble();
        double s = (a + b + c) / 2;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        System.out.println("Area of triangle is " + area);
        scanner.close();
    }
}
This method calculates area using all three sides without needing height, but requires more input and computation.
Using fixed values without input
java
public class TriangleAreaFixed {
    public static void main(String[] args) {
        double base = 5;
        double height = 8;
        double area = 0.5 * base * height;
        System.out.println("Area of triangle is " + area);
    }
}
This approach is simpler but only works for fixed known values, no user input.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations (input reading, multiplication, division), so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

The base-height formula is fastest and simplest; Heron's formula is slower due to square root calculation and more inputs.

ApproachTimeSpaceBest For
Base and Height FormulaO(1)O(1)Simple cases with known height
Heron's FormulaO(1)O(1)When only sides are known
Fixed ValuesO(1)O(1)Quick calculation without user input
💡
Always use double type for base and height to handle decimal values accurately.
⚠️
Beginners often forget to multiply by 0.5, which results in double the actual area.