Home »
Java Programs »
Core Java Example Programs
Java program to find Area of Triangle
Area of Triangle in Java - This program will read base and height of the Triangle and Calculate Area of the Triangle.
To find Area of the Triangle - Multiply Triangle Base width with Triangle Height.
Area of Triangle using Java program
//Java program to find Area of Triangle.
import java.util.*;
public class AreaTriangle{
public static void main(String []args){
double base,height,area;
Scanner sc=new Scanner(System.in);
//Read Length and Width of Triangle
System.out.print("Enter Base Widht: ");
base=sc.nextDouble();
System.out.print("Enter Height: ");
height=sc.nextDouble();
//Calculate Area
area = (base*height)/2;
//Print Result
System.out.println("Area of Triangle: " + area);
}
}
Output
me@linux:~$ javac AreaTriangle.java
me@linux:~$ java AreaTriangle
Enter Base Widht: 2.3
Enter Height: 32.2
Area of Triangle: 37.03
Using Function/Method
//Java program to find Area of Triangle.
import java.util.*;
public class AreaTriangle{
//Function to find Area of Trianle
public static double AreaOfTriangle(double b, double h)
{
double area;
area= (b*h)/2;
return area;
}
public static void main(String []args){
double base,height,area;
Scanner sc=new Scanner(System.in);
//Read Length and Width of Triangle
System.out.print("Enter Base Widht: ");
base=sc.nextDouble();
System.out.print("Enter Height: ");
height=sc.nextDouble();
//Find Area - Calling Functionn
area= AreaOfTriangle(base, height);
//Print Result
System.out.println("Area of Triangle: " + area);
}
}
Core Java Example Programs »