How to compute the hypotenuse h of the triangle in Python

1 Answer

0 votes
import math

# Legs of the triangle
a = 7
b = 5

# Method 1: Using math.sqrt and math.pow
h1 = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
print(f"The hypotenuse (h) is: {h1:.6f}")

# Method 2: Using math.sqrt 
h2 = math.sqrt(a * a + b * b)
print(f"The hypotenuse (h) is: {h2:.6f}")

# Method 3: Using math.hypot 
h3 = math.hypot(a, b)
print(f"The hypotenuse (h) is: {h3:.6f}")



'''
run

The hypotenuse (h) is: 8.602325
The hypotenuse (h) is: 8.602325
The hypotenuse (h) is: 8.602325

'''

 



answered Jun 29, 2025 by avibootz
...