Ask Question
11 July, 17:06

Write a Java program that prompts the user to enter integer values for the sides of a triangle and then displays the values and the type of triangle they represent. If the user enters values that do not make a valid triangle, or if any values are not greater than zero, your program should display the values entered as well the following error message: The values entered do not make a valid triangle.

+5
Answers (1)
  1. 11 July, 19:37
    0
    import java. util. Scanner;

    public class triangle {

    public static void main (String [] args) {

    int sideOne, sideTwo, sideThree;

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter the first side of the triangle");

    sideOne = in. nextInt ();

    System. out. println ("Enter the secon side of the triangle");

    sideTwo = in. nextInt ();

    System. out. println ("Enter the third side of the triangle");

    sideThree = in. nextInt ();

    if (sideOne<=0||sideTwo<=0|| sideThree<=0) {

    System. out. println (" The Values enter are "+sideOne + "," + sideThree + ","+sideThree + " These values don't make a valid triangle");

    }

    else if ((sideOne + sideTwo> sideThree) || (sideOne+sideThree > sideTwo) || (sideThree+sideTwo > sideOne))

    {

    System. out. println ("The triangle is Valid");

    }

    else {

    System. out. println (" The Values enter are "+sideOne + "," + sideTwo + ","+sideThree + " These values don't make a valid triangle");

    }

    }

    Explanation:

    for a Triangle to be Valid one of the three sides of the triangle must greater than the other two sides. The code above enforces this condition using an if statement in combination with the Or Operator

    The following conditions where enforced

    side one, side two and side three! = 0

    Side One + Side Three > Side Two

    Side Three + Side Two> Side One

    Side Two + Side One > Side Three
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a Java program that prompts the user to enter integer values for the sides of a triangle and then displays the values and the type of ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers