In: Computer Science
Right Triangle:If the square of one side equals the sum of the
squares of the other
two sides. in python
To check if the given triangle is right angled or not, we have to first identify the longest side. Once it is identified we can consider it as the LHS of the pythogaras equation. The sum of the square of the other 2 sides are then considered as RHS. Both LHS and RHS are comapred to check whether the given triangle is right angled.
Code:
import math
# Calculate the right side of the pythogaras equation
def rightsidecal(hypo, side1, side2, side3):
if hypo == int(side3): # If side3 is the hypotenuse
rightcal = (math.pow(int(side1), 2) + math.pow(int(side2), 2))
return rightcal
elif hypo == int(side2): # If side2 is the hypotenuse
rightcal = (math.pow(int(side1), 2) + math.pow(int(side3), 2))
return rightcal
elif hypo == int(side1): # If side1 is the hypotenuse
rightcal = (math.pow(int(side2), 2) + math.pow(int(side3), 2))
return rightcal
# Compare the lhs and rhs of the pythogaras equation and output if the triangle is right angled or not
def comp(lhs, rhs):
if lhs == rhs:
print("Right Angled Triangle")
else:
print("Triangle is not Right Angled")
def main():
side1, side2, side3 = input('Enter the length of sides of the triangle (Space seperated): ').split() # Input the length of 3 sides of the triangle
hypo = int(max(side1, side2, side3)) # Hypotensue is the longest side of a right angle triangle
lhs = math.pow(hypo, 2) # Square of the hypotenuse
rhs = rightsidecal(hypo, side1, side2, side3)
comp(lhs, rhs)
main()
Output: