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:
import java.util.Scanner; //Scanner class helps to get input from user
class triangle {
public static void main(String[] args) {
Scanner s = new Scanner(System.in); //Scanner class object declaration
System.out.println("Enter the base of triangle: ");
float b = s.nextFloat(); // takes float input base from keyboard or user or run time
System.out.println("Enter the height of triangle: ");
float h = s.nextFloat(); // takes float input height from keyboard or user or run time
float area = 0.5 * b * h;
System.out.println("Area of Triangle: "+ area);
}
}
Output:
Enter the base of triangle: 3 Enter the height of triangle: 4 Area of Triangle: 6.00