Java program to find area of a triangle|Java program to find area of a triangle Latest update in 2019













Basic Java programming exercises

Java Tutorial Java Exercises Data Structures


Write a Java program to input base and height of a triangle and find area of the given triangle. How to find area of a triangle in Java programming. Logic to find area of a triangle in Java program.








Required knowledge

Arithmetic operator, Variables and expressions, Data types, Basic input/output


Area of a triangle

Area of a triangle is given by formula.



how to find area of triangle,java program to find area of a triangle,java program to find area and perimeter of a triangle,c program to find area of a triangle.,how to find a area of triangle in java,program to find the area of a circle,find area of a triangle in java,program to find the area and perimeter of a circle,java area of a triangle,java

Where b is base and h is height of the triangle.


Logic to find area of a triangle

Below is the step by step descriptive logic to find area of a triangle.

  1. Input base of the triangle. Store in some variable say base.

  2. Input height of the triangle. Store in some variable say height.

  3. Use triangle area formula to calculate area i.e. area = (base * height) / 2.

  4. Print the resultant value of area.


Program to find area of a triangle

/** * Java program to find area of a triangle if base and height are given */ import java.util.Scanner; class Test { public static void main(String args[]) { double base, height, area; Scanner op=new Scanner(System.in); /* Input base and height of triangle */ System.out.print("Enter base of the triangle: "); base=op.nextDouble(); System.out.print("Enter height of the triangle: "); height=op.nextDouble(); /* Calculate area of triangle */ area = (base * height) / 2; /* Print the resultant area */ System.out.println("Area of the triangle = "+area+" sq. units"); } }




Output:

Enter base of the triangle: 2 Enter height of the triangle: 5 Area of the triangle = 5.0 sq. units