24.1k views
3 votes
Read 3 integer numbers from the user; these numbers represent the lengths of 3 straight lines. From the 3 lengths, determine whether a triangle can be formed with the corresponding lines. If it is possible to form a triangle, determine whether the triangle is a right triangle or not. The rules to be used are: 1. To form a triangle where all lines’ ends meet (form vertices), the sum of the lengths of two sides must be greater than the length of the third side. This short video illustrates this concept: How to determine if the three sides make up a triangle. 2. Per the Pythagorean theorem: in a straight triangle, the square of the length of the longest side (the hypotenuse) is equal to the sum of the squares of the lengths of the other two sides. 3. The user can enter the lengths of each line in any order, i.e.: you may not assume that the first length is the longest or the smallest or the middle value.

1 Answer

4 votes

Answer:

Written in Python

print("Enter three sides of a triangle: ")

length = []

for i in range(0,3):

inp = int(input(""))

length.append(inp)

length.sort()

if length[1]+length[2] > length[0] and length[0] + length[2] > length[1] and length[0] + length[1] > length[2]:

print("Triangle")

if length[2]**2 == length[0]**2 + length[1] **2:

print("Right Angled")

else:

print("Not Right Angled")

else:

print("Not Triangle")

Explanation:

This line prompts user for sides of triangle

print("Enter three sides of a triangle: ")

This line declares an empty list

length = []

The following iteration gets user input

for i in range(0,3):

inp = int(input(""))

length.append(inp)

This line sorts user input

length.sort()

The following if condition checks if user input is triangle

if length[1]+length[2] > length[0] and length[0] + length[2] > length[1] and length[0] + length[1] > length[2]:

The following is executed is the if condition is true

print("Triangle")

The following if condition checks if user input forms a right angled triangle

if length[2]**2 == length[0]**2 + length[1] **2:

print("Right Angled")

else:

print("Not Right Angled")

This is executed if user input is not a triangle

else:

print("Not Triangle")

User Jason Sparc
by
4.8k points