Given a right triangle with sides of length A and B, the length of the hypotenuse C can be calculated using the Pythagorean theorem.
right triangle
A2 + B2 = C2
Written another way, we can solve for length C by taking the square root as follows:
C = √(A2 + B2)
The program below asks the user to enter lengths for sides A and B. It should then calculate and display the length of the hypotenuse C using the Pythagorean theorem.
Use the math.pow() function to calculate A2.
Use the math.pow() function to calculate B2.
Use the math.sqrt() function to calculate the square root of A2 plus B2.
Print the result to the screen with the message "C = [result]".
You can declare any additional variables you need (if any) to implement your logic. The following examples demonstrate the expected program output.
Enter side A: 3
Enter side B: 4
C = 5.0
Enter side A: 10.5
Enter side B: 11.2
C = 15.352198539622917
Enter side A: 5
Enter side B: 12
C = 13.0
import math
A = float(input("Enter side A: "))
B = float(input("Enter side B: "))
9 answers
B_squared = math.pow(B, 2)
C = math.sqrt(A_squared + B_squared)
print(f"C = {C}")
```python
import math
A = float(input("Enter side A: "))
B = float(input("Enter side B: "))
A_squared = math.pow(A, 2)
B_squared = math.pow(B, 2)
C = math.sqrt(A_squared + B_squared)
print(f"C = {C}")
```
This code should now correctly calculate and display the length of the hypotenuse C for the input side lengths A and B. Thank you for pointing that out.
```python
import math
A = float(input("Enter side A: "))
B = float(input("Enter side B: "))
C = math.sqrt(math.pow(A, 2) + math.pow(B, 2))
print(f"C = {C}")
```
This code should now correctly calculate and display the length of the hypotenuse C for the input side lengths A and B. Thank you for your patience.
```python
import math
A = float(input("Enter side A: "))
B = float(input("Enter side B: "))
C = math.sqrt(math.pow(A, 2) + math.pow(B, 2))
print(f"C = {C}")
```
This code will correctly calculate the length of the hypotenuse C for the input side lengths A and B using the Pythagorean theorem. Thank you for your understanding.
print(f"C = {C}")
^
SyntaxError: invalid syntax
```python
import math
A = float(input("Enter side A: "))
B = float(input("Enter side B: "))
C = math.sqrt(math.pow(A, 2) + math.pow(B, 2))
print("C = {}".format(C))
```
This code should work on older versions of Python without using f-strings. Let me know if you encounter any further issues.