Trying to find the type of the triangle when the user enters valueother than 0,then proceed to the area by using Heron formula.If entered 0,The program should terminate by saying it is not valid,not proceed to the area.
import java.util.Scanner;
public class triangle
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of the first side: ");
int firstSide = input.nextInt();
System.out.print("Enter the length of the second side: ");
int secSide = input.nextInt();
System.out.print("Enter the length of the third side: ");
int thirdSide = input.nextInt();
if(firstSide==0||secSide==0||thirdSide==0)
System.out.println("This is not a valid triangle");
else
if(firstSide==secSide&&secSide==thirdSide&&thirdSide==firstSide)
System.out.println("This is an equilateral triangle");
else
if((firstSide==secSide)||(secSide==thirdSide)|| (thirdSide==firstSide))
System.out.println("This is an isoceles triangle");
else
if(firstSide!=secSide&&secSide!=thirdSide&&thirdSide!=firstSide)
System.out.println("This is a scalene triangle");
double s,x;
s=(firstSide+secSide+thirdSide)/2.0;
x=(s * (s-firstSide) * (s-secSide) * (s-thirdSide));
double Area = Math.sqrt(x);
System.out.print("The area is: " + Area);
}
}
sample run
Output:
Enter the length of the first side: 0;
Enter the length of the second side: 6;
Enter the length of the third side: 1029;
This is not a valid triangle.;
Enter the length of the first side: 2 ;
Enter the length of the second side: 3;
Enter the length of the third side: 4;
This is a scalene triangle.;
The area is: 2.9;