Java program to calculate area of Triangle

In this tutorial, we will learn how to write a Java program to calculate the area of the Triangle.
We will read input from the console using Scanner class.

Java program to calculate the area of Triangle

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java program to calculate area of Triangle
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Enter the width of the Triangle:");
            double base = scanner.nextDouble();

            System.out.println("Enter the height of the Triangle:");
            double height = scanner.nextDouble();

            // Area = (width*height)/2
            double area = (base * height) / 2;
            System.out.println("Area of Triangle is: " + area);
        }
    }
}
Output:
Enter the width of the Triangle:
10
Enter the height of the Triangle:
5
Area of Triangle is: 25.0

Related Java Programs


Comments