Pythagorean Theorem in Python calc: find a, b=n/a, c=n/a

In this tutorial, We’ll discuss how you can calculate the side of any right angle triangle using Pythagorean Theorem in Python. Before that, let’s get quick look at Pythagorean Theorem:

Pythagorean Theorem: The Pythagorean Theorem states that – in the right angle triangle the square of the hypotenuse is equal to the sum of the squares of the other two sides.

if a, b and c are the 3 sides of any right angle triangle and c is hypotenuse then,

C2 = a2 + b2

First, we need to import sqrt() function from math library of python in order to find square-root of any number. We also provide a solution without sqrt() function below but first, look at how we can do it with the help of sqrt() function.

from math import sqrt

Here we assume a, b and c are three sides of any right angle triangle and c is the hypotenuse of that triangle. Hence according to Pythagorean Theorem :

C2 = a2 + b

from math import sqrt

print('''We Assume a,b,and c are three sides of triangle and c is hypotenuse''')
side_find = input("Enter the side you want to find : ")

if side_find == "a":
    side_b = int(input("Enter the length of side b : "))
    side_c = int(input("Enter the length of side c : "))
    side_find = sqrt(side_c**2 - side_b**2)
    print(f"The length of side a : {side_find}")
elif side_find == "b":
    side_a = int(input("Enter the length of side a : "))
    side_c = int(input("Enter the length of side c : "))
    side_find = sqrt(side_c**2 - side_a**2)
    print(f"The length of side b : {side_find}")
else:
    side_a = int(input("Enter the length of side a : "))
    side_b = int(input("Enter the length of side b : "))
    side_find = sqrt(side_a**2 + side_b**2)
    print(f"The length of side c : {side_find}")

 Output :

We Assume a,b,and c are three sides of triangle and c is hypotenuse
Enter the side you want to find : a
Enter the length of side b : 3
Enter the length of side c : 5
The length of side a : 4.0

Here in the above code i want to find side “a” so i entered a , the length of b and c are 3 and 5 respectively , Hence by this we get our side a = 4.0 .

With sqrt() function

This is simple program, here we use “**” to find square root rather than sqrt() function.

print("We Assume a,b,and c are three sides of triangle and c is hypotenuse")
side_find = input("Enter the side you want to find : ")

if side_find == "a":
    side_b = int(input("Enter the length of side b : "))
    side_c = int(input("Enter the length of side c : "))
    side_find = (side_c**2 - side_b**2)**(1/2)
    print(f"The length of side a : {side_find}")
elif side_find == "b":
    side_a = int(input("Enter the length of side a : "))
    side_c = int(input("Enter the length of side c : "))
    side_find = (side_c**2 - side_a**2)**(1/2)
    print(f"The length of side b : {side_find}")
else:
    side_a = int(input("Enter the length of side a : "))
    side_b = int(input("Enter the length of side b : "))
    side_find = (side_a**2 + side_b**2)**(1/2)
    print(f"The length of side c : {side_find}")
We Assume a,b,and c are three sides of triangle and c is hypotenuse
Enter the side you want to find : a
Enter the length of side b : 3
Enter the length of side c : 5
The length of side a : 4.0

Here again, we got a = 4.0, when we passed b=3 and c=5, Hence no matter what method you used your answer will be the same.

I hope this will be helpful for you.

Leave a Reply

Your email address will not be published. Required fields are marked *