# Sides of right angled triangle from given area and hypotenuse in Python

In this article, we will learn how to find all the sides of a right-angled triangle from a given area and hypotenuse in Python.

Examples

Input: hypotenuse = 10, area = 24 Output: Base = 6, Height = 8 Input: hypotenuse = 5, area = 10 Output: No triangle possible

**Some of the property of a right-angled triangle are**

Let’s consider a right-angle triangle with height a, base b the hypotenuse c will be

c² = a² + b²

A right-angle triangle will have a maximum area when both the base and height to each other.

## All the sides of a right-angled triangle from a given area and hypotenuse in Python

1. Firstly create a function area() that takes base, the hypotenuse as arguments.

- Calculate the height using the base and hypotenuse height = math.sqrt(hypotenuse*hypotenuse – base*base).
- return the area of the triangle 0.5 * height * base.

2. Now create a function idesOfRightAngleTriangle() that calculates the sides of the triangle

- Firstly, calculate the maximum possible side when the area is maximum. and calculate the area using the area() function.
- Compare given the area a with the maximum area, if a>maxArea then print “No possible”.
- Using binary search calculate the base and the height of the triagle.

import math def area(base, hypotenuse): height = math.sqrt(hypotenuse*hypotenuse - base*base) return 0.5 * base * height def sidesOfRightAngleTriangle(h, a): hsqrt = h*h maxAreaSide = math.sqrt(hsqrt/2.0) maxArea = area(maxAreaSide, h) if (a>maxArea): print("No possible") return low = 0.0 high = maxAreaSide while (abs(high-low)>1e-6): base = (low+high)/2.0 if (area(base, h) >= a): high = base else: low = base height = math.ceil(math.sqrt(hsqrt - base*base)) base = math.floor(base) print("Base: ", base) print("Height: ", height) h = int(input("Enter the hypotenuse: ")) a = int(input("Enter the area: ")) sidesOfRightAngleTriangle(h, a)

Output

Enter the hypotenuse: 5 Enter the area: 6 Base: 3 Height: 4 Enter the hypotenuse: 5 Enter the area: 7 No possible

Also, read

## Leave a Reply