1.6k views
3 votes
Write a function is_rightangled which, given the length of three sides of a triangle, will determine whether the triangle is right-angled. Assume that the third argument to the function is always the longest side. It will return True if the triangle is right-angled, or False otherwise. Hint: floating point arithmetic is not always exactly accurate, so it is not safe to test floating point numbers for equality. If a good programmer wants to know whether x is equal or close enough to y, they would probably code it up as

1 Answer

4 votes

Answer:

Written in Python:

def is_rightangled(a,b,c):

if abs(c**2 - (a**2 + b**2) < 0.001):

return "True"

else:

return "False"

a = float(input("Side 1: "))

b = float(input("Side 2: "))

c = float(input("Side 3: "))

print(is_rightangled(a,b,c))

Step-by-step explanation:

The function is defined here

def is_rightangled(a,b,c):

The following if statement implements Pythagoras theorem
c^2 = a^2 + b^2 by checking if the left hand side is approximately equal to the right hand side

if abs(c**2 - (a**2 + b**2) < 0.001):

return "True"

else:

return "False"

If the specified condition is met, the if function returns true; Otherwise, it returns false.

The main function starts here

The next three line prompts user for the sides of the triangle

a = float(input("Side 1: "))

b = float(input("Side 2: "))

c = float(input("Side 3: "))

The next line calls the defined function

print(is_rightangled(a,b,c))

User Dave Haynes
by
4.7k points