Pythagoras’ Theorem

Pythagoras’ Theorem

The Pythagoras’ theorem for a right-angle triangle can be written as a2+b2 = c2, where a and b are the sides of a right angle triangle and c is its hypotenuse.  Write a program to compute the hypotenuse, given the  sides a and b of a triangle by using a user defined function and this function should return the computed hypotenuse.  The function signature should be:  hypotenuse(triangle_side1,triangle_side2) .  Refer the sample input/output for more details.

Note: 

  • You can use math.sqrt(x) to compute the square root of x.
  • The triangle side values should be greater than 0, else display the message  “Invalid input”

Sample input statement 1:

Enter the triangle side 1: 3

Enter the triangle side 2: 4

Sample output statement 1:

Hypotenuse = 5

Sample input statement 2:

Enter the triangle side 1: 5

Enter the triangle side 2: 12

Sample output statement 2:

Hypotenuse = 13

Sample input statement 3:

Enter the triangle side 1: 0

Enter the triangle side 2: 12

Sample output statement 3:
Invalid Input

Sample input statement 4:

Enter the triangle side 1: 10

Enter the triangle side 2: -1

Sample output statement 4:
Invalid Input

Code :-

import math
n=int(input("Enter the triangle side 1: "))
m=int(input("Enter the triangle side 2: "))
if n>0 and m>0:
    x=round(math.sqrt(pow(n,2)+pow(m,2)))
    print("Hypotenuse = "+str(x))
else:
    print("Invalid Input")

Leave a Comment

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

x