Python Program to Find Hypotenuse

The hypotenuse is the longest side of a right triangle. It is also the side that is not opposite the right angle.

If you’re trying to find the hypotenuse of a right-angled triangle, you can use the following equation:

h = (a^2 + b^2)

Where h is the hypotenuse, a and b are the lengths of the other two sides, and x is the angle at which the triangle is drawn.

Program Code

from math import sqrt

a = float(input('Enter the length of the shorter triangle side: '))
b = float(input('Enter the length of the other shorter triangle side: '))

c = sqrt(a**2 + b**2)

print('The length of the hypotenuse is', c)

Input Given:

Enter the length of the shorter triangle side: 3
Enter the length of the other shorter triangle side: 4

Expected Output:

The length of the hypotenuse is 5.0

Code Explanation

  1. We import the sqrt function from the math module.
  2. We ask the user to enter the length of the shorter triangle side.
  3. We ask the user to enter the length of the other shorter triangle side.
  4. We calculate the length of the hypotenuse using the Pythagorean theorem.
  5. We print the length of the hypotenuse.

Leave a Comment

Share to...