write a program in java to find the area of triangle Take height and base from user
Answers
The given code is written in Java.
import java.util.*;
public class TriangleArea{
public static void main(){
Scanner _=new Scanner(System.in);
float height,base,area;
System.out.print("Enter height of the triangle: ");
height=_.nextFloat();
System.out.print("Enter the base of the triangle: ");
base=_.nextFloat();
_.close();
area=base * height * 1/2.0f; // As A = bh/2
System.out.println("The area of the triangle is: "+area+" sq units.");
}
}
- Accept the height and base of the triangle.
- Calculate area, area = 1/2 * height * base.
- Display area.
See the attachment for output.
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int b,h;
double ar=0;
System.out.println("Enter the base of the triangle:");
b=in.nextInt();
System.out.println("Enter the height of the triangle");
h=in.nextInt();
ar=1.0/2.0*b*h;
System.out.println("The area of triangle="+ar);
}
}