Pythagorean Theorem:
Pythagorean theorem python: According to the Pythagorean Theorem, the square of the hypotenuse in a right-angle triangle is equal to the sum of the squares of the other two sides.
If the three sides of a right-angle triangle are a, b, and c, and c is the hypotenuse, then
c^2 = a^2 + b^2
Calculation of Pythagorean Theorem in Python
Method #1: Using sqrt() Function (User Input)
Approach:
- Import sqrt function from math module using the import keyword
- Give some random side(a,b,c) of a right-angled triangle as user input using the input() function and store it in a variable.
- Check if the given side is ‘a’ using the if conditional statement
- If it is true, then give the sides b, c as user input using the int(input()) function and store them in separate variables
- Calculate the corresponding hypotenuse side(a) with the given two sides using the given mathematical formula, srqt() function and store it in another variable.
- Print the hypotenuse(side-a) of the right-angled triangle
- Check if the given side is ‘b’ using the elif conditional statement.
- If it is true, then give the sides a, c as user input using the int(input()) function and store them in separate variables
- Calculate the corresponding hypotenuse side(a) with the given two sides using the given mathematical formula, srqt() function and store it in another variable.
- Print the hypotenuse(side-b) of the right-angled triangle
- Else check if the given side is ‘c’ using the else statement
- If it is true, then give the sides a, b as user input using the int(input()) function and store them in separate variables
- Calculate the corresponding hypotenuse side(a) with the given two sides using the given mathematical formula, srqt() function and store it in another variable.
- Print the hypotenuse(side-c) of the right-angled triangle.
- The Exit of the Program.
Below is the implementation:
# Import sqrt function from math module using the import keyword from math import sqrt # Give some random side(a,b,c) of a right angled triangle as user input # using the input() function and store it in a variable. gvn_side = input("Enter some random side(a,b,c) of a right angled triangle: ") # Check if the given side is 'a' using the if conditional statement if gvn_side == "a": # If it is true, then give the sides b, c as user input using the # int(input()) function and store them in separate variables gvn_b = int(input("Enter some random number(side-b)= ")) gvn_c = int(input("Enter some random number(side-c)= ")) # Calculate the corresponding hypotenuse side(a) with the given two sides # using the given mathematical formula, srqt() function and store it in another variable. hypotenuse_side= sqrt(gvn_c**2 - gvn_b**2) # Print the hypotenuse(side-a) of the right-angled triangle print("The hypotenuse(side-a) of the right-angled triangle = ",hypotenuse_side) # Check if the given side is 'b' using the elif conditional statement elif gvn_side == "b": # If it is true, then give the sides a, c as user input using the # int(input()) function and store them in separate variables gvn_a = int(input("Enter some random number(side-a)= ")) gvn_c = int(input("Enter some random number(side-c)= ")) # Calculate the corresponding hypotenuse side(a) with the given two sides # using the given mathematical formula, srqt() function and store it in another variable. hypotenuse_side = sqrt(gvn_c**2 - gvn_a**2) # Print the hypotenuse(side-b) of the right-angled triangle print("The hypotenuse(side-b) of the right-angled triangle = ",hypotenuse_side) # Else check if the given side is 'c' using the else statement else: # If it is true, then give the sides a, b as user input using the # int(input()) function and store them in separate variables gvn_a = int(input("Enter some random number(side-a)= ")) gvn_b = int(input("Enter some random number(side-b)= ")) # Calculate the corresponding hypotenuse side(a) with the given two sides # using the given mathematical formula, srqt() function and store it in another variable. hypotenuse_side = sqrt(gvn_a**2 + gvn_b**2) # Print the hypotenuse(side-c) of the right-angled triangle print("The hypotenuse(side-c) of the right-angled triangle = ",hypotenuse_side)
Output:
Enter some random side(a,b,c) of a right angled triangle: b Enter some random number(side-a)= 2 Enter some random number(side-c)= 5 The hypotenuse(side-b) of the right-angled triangle = 4.58257569495584
Method #2: Using Mathematical Formula (User Input)
Approach:
- Import sqrt function from math module using the import keyword
- Give some random side(a,b,c) of a right-angled triangle as user input using the input() function and store it in a variable.
- Check if the given side is ‘a’ using the if conditional statement
- If it is true, then give the sides b, c as user input using the int(input()) function and store them in separate variables
- Calculate the corresponding hypotenuse side(a) with the given two sides using the given mathematical formula and store it in another variable.
- Print the hypotenuse(side-a) of the right-angled triangle
- Check if the given side is ‘b’ using the elif conditional statement.
- If it is true, then give the sides a, c as user input using the int(input()) function and store them in separate variables
- Calculate the corresponding hypotenuse side(a) with the given two sides using the given mathematical formula and store it in another variable.
- Print the hypotenuse(side-b) of the right-angled triangle
- Else check if the given side is ‘c’ using the else statement
- If it is true, then give the sides a, b as user input using the int(input()) function and store them in separate variables
- Calculate the corresponding hypotenuse side(a) with the given two sides using the given mathematical formula and store it in another variable.
- Print the hypotenuse(side-c) of the right-angled triangle.
- The Exit of the Program.
Below is the implementation:
# Import sqrt function from math module using the import keyword from math import sqrt # Give some random side(a,b,c) of a right angled triangle as user input # using the input() function and store it in a variable. gvn_side = input("Enter some random side(a,b,c) of a right angled triangle: ") # Check if the given side is 'a' using the if conditional statement if gvn_side == "a": # If it is true, then give the sides b, c as user input using the # int(input()) function and store them in separate variables gvn_b = int(input("Enter some random number(side-b)= ")) gvn_c = int(input("Enter some random number(side-c)= ")) # Calculate the corresponding hypotenuse side(a) with the given two sides # using the given mathematical formula and store it in another variable. hypotenuse_side= (gvn_c**2 - gvn_b**2)**(1/2) # Print the hypotenuse(side-a) of the right-angled triangle print("The hypotenuse(side-a) of the right-angled triangle = ",hypotenuse_side) # Check if the given side is 'b' using the elif conditional statement elif gvn_side == "b": # If it is true, then give the sides a, c as user input using the # int(input()) function and store them in separate variables gvn_a = int(input("Enter some random number(side-a)= ")) gvn_c = int(input("Enter some random number(side-c)= ")) # Calculate the corresponding hypotenuse side(a) with the given two sides # using the given mathematical formula and store it in another variable. hypotenuse_side = (gvn_c**2 - gvn_a**2)**(1/2) # Print the hypotenuse(side-b) of the right-angled triangle print("The hypotenuse(side-b) of the right-angled triangle = ",hypotenuse_side) # Else check if the given side is 'c' using the else statement else: # If it is true, then give the sides a, b as user input using the # int(input()) function and store them in separate variables gvn_a = int(input("Enter some random number(side-a)= ")) gvn_b = int(input("Enter some random number(side-b)= ")) # Calculate the corresponding hypotenuse side(a) with the given two sides # using the given mathematical formula and store it in another variable. hypotenuse_side = (gvn_a**2 + gvn_b**2)**(1/2) # Print the hypotenuse(side-c) of the right-angled triangle print("The hypotenuse(side-c) of the right-angled triangle = ",hypotenuse_side)
Output:
Enter some random side(a,b,c) of a right angled triangle: a Enter some random number(side-b)= 2 Enter some random number(side-c)= 6 The hypotenuse(side-a) of the right-angled triangle = 5.656854249492381