Trying to determine the area of a triangle. Prompted the user for 3 numbers(doubles) and calculated the area of a triangle within the program. The area must be a number of at least 3 decimal places. Yet, the area keeps coming out to 0. What am I doing wrong?
public static void main (String [] args) {
double sideA = 0.0;
double sideB = 0.0;
double sideC = 0.0;
int s = 1/2;
double area = 0.000;
Scanner scnr = new Scanner(System.in);
System.out.println("Enter Side A: ");
sideA = scnr.nextInt();
System.out.println("Enter Side B: ");
sideB = scnr.nextInt();
System.out.println("Enter Side C: ");
sideC = scnr.nextInt();
DecimalFormat fmt = new DecimalFormat("0.###");
area = Math.sqrt((s * (s - sideA) * (s - sideB) * (s - sideC)));
System.out.println("The area of the triangle is: " + fmt.format(area));
return;
This may help, I have taken your code and worked on it and this is what mine looks like:
public static void main (String [] args) {
double base;
double height;
double s = 2;
double area;
Scanner scnr = new Scanner(System.in);
System.out.print("Enter length for the Base: ");
base = scnr.nextInt();
System.out.println("Enter Height of the triangle: ");
height = scnr.nextInt();
DecimalFormat fmt = new DecimalFormat("0.###");
area = (height*base) / s;
System.out.println("The area of the triangle is: " + fmt.format(area));
}
This will printout 157.5 as your area. You were close to it but you are combining the formula for the perimeter and the formula for the area together and thats why it didn't work.