Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. //This program calcuates the perimeter and the area of a triangle, given 3 lenghts.
  2. // Author: Eyal Oren
  3. // Date:
  4.  
  5. import java.util.Scanner;
  6. public class Triangle
  7. {
  8. public static void main(String[] args)
  9. {
  10. Scanner scan = new Scanner(System.in); //Creating a scanner to take input from user.
  11. System.out.println("This program calculates the area "
  12. + "and the perimeter of the given triange. ");
  13. System.out.println("Please enter the three lenghts "
  14. + "of the triangle's sides." );
  15.  
  16. //defining the three lenghts type, perimeter type, area type, and a variable for future calculation, and asking the user for their value.
  17. int a = scan.nextInt();
  18. int b = scan.nextInt();
  19. int c = scan.nextInt();
  20. int peri, area, half_peri;
  21.  
  22.  
  23. //Creating restricions for the lenghts of the triangle.
  24. if (a > 0 && b > 0 && c > 0) //Declaring the lenghts won't be negative or zero.
  25. { if (a + b > c && b + c > a && a + c > b) //Declaring that the lengths will fulfill the triangle inequallity.
  26. //If all terms are vaild, proceeding to calculations.
  27. { peri = a + b + c;
  28.  
  29. half_peri = peri/2;
  30.  
  31. area = half_peri*(half_peri-a)*(half_peri-b)*(half_peri-c);
  32.  
  33. System.out.println("The perimeter is: " + peri + " The area is: " + area);
  34.  
  35. }
  36. else
  37. System.out.println("The lenghts doesn't fulfill the triangle inequallity, you entered: " + a + " " + b + " " + c);
  38. }
  39.  
  40. else
  41. System.out.println("The lenghts must be positive, you entered :" + a + " " + b + " " + c);
  42.  
  43.  
  44.  
  45. } //end of method main
  46. } //end of class Triangle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement