Here, you will learn how to calculate the Area of a Triangle using the Formula [(Base* Height)/2]. The program takes the user input for the Base and Height of a triangle, calculates the result, and displays the result on the screen.
If you do not know about the operators in java, click the link below to learn about operators as this program is using one.
Java Program to Calculate the Area of a Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //Area of a traingle import java.util.Scanner; public class AreaOfTriangle { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the base of the Triangle:"); float base = sc.nextFloat(); System.out.println("Enter the height of the Triangle:"); float height = sc.nextFloat(); //Area = (base*height)/2 float area = (base* height)/2; System.out.println("Area of the Triangle is: " + area); } } |
Output:
Enter the base of the Triangle:
8
Enter the height of the Triangle:
10
Area of the Triangle is: 40.0