FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Calculate area of Triangle


Here we will see how to calculate area of triangle. We will see two following programs to do this:

1) Program 1: Prompt user for base-width and height of triangle.
2) Program 2: No user interaction: Width and height are specified in the program itself.

Program 1:

import java.util.Scanner;
class AreaTriangleDemo {
   public static void main(String args[]) {   
      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:
2
Enter the height of the Triangle:
2
Area of Triangle is: 2.0

Program 2:

class AreaTriangleDemo2 {
   public static void main(String args[]) {   
      double base = 20.0;
      double height = 110.5;
      double area = (base* height)/2;
      System.out.println("Area of Triangle is: " + area);      
    }
}

Output:

Area of Triangle is: 1105.0

You may also Find this interesting

Program to take users input

Program to add two numbers

Program to find even number

Program to find odd numbers

Program to find even or odd numbers

Program to Display average of numbers

Program to Display Fabinocci Series

Program to Display Random Numbers

Program to Display Largest of Three numbers