A Program to Find the Area of a Triangle

In this example, we are giving a program to find the area of a triangle. The rule of finding the area of a triangle is: A = 0.5 * base * height.


Program to find the area of a triangle:

  1. import java.util.Scanner; //Scanner class helps to get input from user
  2. class triangle {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in); //Scanner class object declaration
  5. System.out.println("Enter the base of triangle: ");
  6. float b = s.nextFloat(); // takes float input base from keyboard or user or run time
  7. System.out.println("Enter the height of triangle: ");
  8. float h = s.nextFloat(); // takes float input height from keyboard or user or run time
  9. float area = 0.5 * b * h;
  10. System.out.println("Area of Triangle: "+ area);
  11. }
  12. }

Output:

Enter the base of triangle: 3
Enter the height of triangle: 4
Area of Triangle: 6.00