Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program calcuates the perimeter and the area of a triangle, given 3 lenghts.
- // Author: Eyal Oren
- // Date:
- import java.util.Scanner;
- public class Triangle
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in); //Creating a scanner to take input from user.
- System.out.println("This program calculates the area "
- + "and the perimeter of the given triange. ");
- System.out.println("Please enter the three lenghts "
- + "of the triangle's sides." );
- //defining the three lenghts type, perimeter type, area type, and a variable for future calculation, and asking the user for their value.
- int a = scan.nextInt();
- int b = scan.nextInt();
- int c = scan.nextInt();
- int peri, area, half_peri;
- //Creating restricions for the lenghts of the triangle.
- if (a > 0 && b > 0 && c > 0) //Declaring the lenghts won't be negative or zero.
- { if (a + b > c && b + c > a && a + c > b) //Declaring that the lengths will fulfill the triangle inequallity.
- //If all terms are vaild, proceeding to calculations.
- { peri = a + b + c;
- half_peri = peri/2;
- area = half_peri*(half_peri-a)*(half_peri-b)*(half_peri-c);
- System.out.println("The perimeter is: " + peri + " The area is: " + area);
- }
- else
- System.out.println("The lenghts doesn't fulfill the triangle inequallity, you entered: " + a + " " + b + " " + c);
- }
- else
- System.out.println("The lenghts must be positive, you entered :" + a + " " + b + " " + c);
- } //end of method main
- } //end of class Triangle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement