Area is the size of the surface of an object. In this article, we will discuss about the formula and java code in solving for the area of a triangle.
Formula
h = height
b = base
A = Area of a triangle
Java Code
public class Area {
public static void main(String[] args) {
// solve for the area of a triangle given the height and width
double height = 3.5;
double width = 5;
double area;
area=height*width/2;
System.out.println(area);
}
}
Output
Or you can also achieve it through user input where a user will provide the value for the height and width.
Java Code
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
// solve for the area of a triangle given the
//height and width will be provided by user
Scanner console=new Scanner(System.in);
System.out.println("Enter the triangle's width: ");
double height = console.nextDouble();
System.out.println("Enter the triangle's base: ");
double width = console.nextDouble();
double area=height*width/2;
System.out.println("The area of the triangle is "+area);
}
}
Output
0 comments:
Post a Comment