Heron's Formula


/ Published in: Java
Save to your folder(s)

Java Heron's formula


Copy this code and paste it in your HTML
  1. import java.util.Scanner;
  2.  
  3. public class HeronsFormula {
  4. public static void main(String[] args){
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("Input the three sides");
  7. double sidea = input.nextDouble();
  8. double sideb = input.nextDouble();
  9. double sidec = input.nextDouble();
  10. System.out.println("The lengths of the sides of your triangle are: " + '\n' + sidea + '\n' + sideb + '\n' + sidec);
  11.  
  12.  
  13. System.out.println("The area of your triangle using Heron's Formula is: " + getArea(sidea,sideb,sidec));
  14.  
  15. }
  16.  
  17. public static double getArea(double a, double b, double c) { //method calculates area
  18. double s = (a + b + c)/2.0; //s = perimeter/2
  19. System.out.println("s is: " + s);
  20. double x = ((s) * (s-a) * (s-b) * (s-c)); //herons formula
  21. double area = Math.sqrt(x); //math function to square the input from herons formula
  22. return area;
  23. }
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.