158k views
1 vote
Write a program that does the following: - Ask user to input lengths of three sides. - Classify it into one of the following: o Equilateral triangle o Isosceles right triangle o Isosceles triangle that is not a right triangle o A triangle that is not an isosceles and not an equilateral For example, an execution could look like

1 Answer

3 votes

Answer:

You did not specify which programming language you use, but you can use if statements to check each using the sides.

- A triangle is equilateral when all the 3 sides are equal. (a = b = c)

- A triangle is isoscles when two of the sides are equal (a = b or b = c or c = a)

- A triangle is right when square of side C is equal to the sum of the squares of the other two sides. (c^2 = a^2 + b^2)

- For the last classification you can use "else"

If you noticed, the condition to have an isosceles triangle is repeated for two of the tasks so we can use the same if statement to check if the triangle is isosceles right or just isoscles with two built-in if statements (if and else)- if it's right then it's isosceles right else it's just isosceles.

And else statement for the last task.

Example code structure:

If triangle is Equilateral then

- Equilateral Triangle

end

If triangle is Isoscles then

if triangle is right then

- Isoscles Right Triangle

end

else (the triangle is not right)

- Isoscles Triangle

end

end

else (the triangle is not Equilateral nor Isoscles)

- Triangle that is not isoscles nor equilateral.

end

Step-by-step explanation:

Here is a code example with java, I hope it helps, although it might not be the programming language you use:

import java.util.Scanner;

public class Triangle {

public static void main(String[] args) {

Scanner input = new

Scanner(System.in);

System.out.print("side a = ");

int a = input.nextInt();

System.out.print("side b = ");

int b = input.nextInt();

System.out.print("side c = ");

int c = input.nextInt();

if (a == b && b == c)

{

System.out.println("Equilateral Triangle);

}

else if (a == b || b == c || a == c) {

if ( (int) math.pow(c, 2) == (int) (math.pow(a, 2) + math.pow(b, 2)))

{

System.out.println("Isoscles Right Triangle");

}

else {

System.out.println("Isoscles triangle that is not a right triangle");

}

}

else {

System.out.println("Triangle that is not Isoscles nor Equilateral");

}

}

}

Let me know if you have any questions.

User Arnaud Leyder
by
3.3k points