Python Program to Find Hypotenuse of Right Triangle
You can find the hypotenuse of a right triangle in Python using the formula
hypotenuse = (side1**2 + side2**2)**0.5, where side1 and side2 are the lengths of the other two sides.Examples
Inputside1 = 3, side2 = 4
Output5.0
Inputside1 = 5, side2 = 12
Output13.0
Inputside1 = 0, side2 = 0
Output0.0
How to Think About It
To find the hypotenuse, think of the right triangle as having two shorter sides. The hypotenuse is the longest side opposite the right angle. Use the Pythagorean theorem which says the square of the hypotenuse equals the sum of the squares of the other two sides. So, square both sides, add them, then take the square root of that sum to get the hypotenuse.
Algorithm
1
Get the lengths of the two shorter sides of the triangle.2
Square each side length.3
Add the two squared values together.4
Calculate the square root of the sum to find the hypotenuse.5
Return or print the hypotenuse value.Code
python
side1 = float(input('Enter length of side 1: ')) side2 = float(input('Enter length of side 2: ')) hypotenuse = (side1**2 + side2**2)**0.5 print('Hypotenuse:', hypotenuse)
Output
Enter length of side 1: 3
Enter length of side 2: 4
Hypotenuse: 5.0
Dry Run
Let's trace the example where side1 = 3 and side2 = 4 through the code.
1
Input sides
side1 = 3, side2 = 4
2
Square sides
3**2 = 9, 4**2 = 16
3
Sum squares
9 + 16 = 25
4
Square root
sqrt(25) = 5.0
5
Output hypotenuse
Hypotenuse: 5.0
| Step | Operation | Value |
|---|---|---|
| 1 | Input sides | side1=3, side2=4 |
| 2 | Square sides | 9, 16 |
| 3 | Sum squares | 25 |
| 4 | Square root | 5.0 |
| 5 | Output | Hypotenuse=5.0 |
Why This Works
Step 1: Square the sides
We use side1**2 and side2**2 to get the squares of the two sides.
Step 2: Add the squares
Adding the squares with side1**2 + side2**2 gives the square of the hypotenuse.
Step 3: Calculate the square root
Taking the square root with **0.5 gives the length of the hypotenuse.
Alternative Approaches
Using math.sqrt() function
python
import math side1 = float(input('Enter side 1: ')) side2 = float(input('Enter side 2: ')) hypotenuse = math.sqrt(side1**2 + side2**2) print('Hypotenuse:', hypotenuse)
This uses the built-in math module for clearer square root calculation.
Using a function to reuse code
python
def find_hypotenuse(a, b): return (a**2 + b**2)**0.5 print('Hypotenuse:', find_hypotenuse(3, 4))
Encapsulates logic in a function for reuse and cleaner code.
Complexity: O(1) time, O(1) space
Time Complexity
The calculation involves a fixed number of arithmetic operations, so it runs in constant time.
Space Complexity
Only a few variables are used, so the space needed is constant.
Which Approach is Fastest?
Both direct exponentiation and using math.sqrt() are equally fast for this simple calculation.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct exponentiation (**0.5) | O(1) | O(1) | Simple quick calculation |
| math.sqrt() function | O(1) | O(1) | Clearer code and readability |
| Function encapsulation | O(1) | O(1) | Reusable code in larger programs |
Use
**0.5 or math.sqrt() to find square roots in Python.Forgetting to square both sides before adding or taking the square root of the sum.