Finding the kind of triangle – Java Program

What it does:

The program prompts the user to enter three lengths for the sides. Depending on the type of sides the user has enter the program will then tell them what kind of triangle the lengths they entered will form. If a triangle cannot be formed which can happen in some cases it will also tell the user.

Code:

 


public class Triangle
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        int side1, side2, side3;

        System.out.println("\nTo determine the type of triangle please enter the lengths of the sides. " +
                           "\nEach length MUST be entered in INCREASING order of size.\n");

        System.out.println("Side 1: ");
        side1 = keyboard.nextInt();

        System.out.println("Side 2: ");
        side2 = keyboard.nextInt();

        System.out.println("Side 3: ");
        side3 = keyboard.nextInt();

        //To check to see if it is an triangle
       // The sum of two smaller sides must be greater than the longest side to be a triangle
        if((side1 + side2 > side3))
        {
            //To check to see if it an Equilateral triangle (3 equal sides)
            if((side1==side2) && (side2==side3))
            {
               System.out.print("\n RESULT: An Equilateral Triangle.");
            }//if

            //To check to see if it is an Isosceles triangle (2 of the same size and 1 different)
            else if ((side1 == side2) & (side2 != side3) || (side2 == side3) & (side3!= side1))
            {
                System.out.print("\n RESULT: An Isosceles Triangle.");
            }//else if

                 //To check to see if it is a Scalene triangle (3 different sizes)
                 else if((side1 != side2) & (side2 != side3))
                 {
                    System.out.print("\n RESULT: A Scalene Triangle.");
                 }//else if

        }//if

        //Sum of two smaller sides is smaller than the longest side
        //When it is not a triangle it will print this.
        else
        {
            System.out.print("\n RESULT: This Triangle is NOT possible.");
        }//else


    }//main
}//class

2 thoughts on “Finding the kind of triangle – Java Program

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s