Find the dimensions of Right angled triangle
Last Updated :
27 Aug, 2022
Given H (Hypotenuse) and A (area) of a right angled triangle, find the dimensions of right angled triangle such that the hypotenuse is of length H and its area is A. If no such triangle exists, print "Not Possible".
Examples:
Input : H = 10, A = 24
Output : P = 6.00, B = 8.00
Input : H = 13, A = 36
Output : Not Possible
Approach:
Before moving to exact solution, let's do some of mathematical calculations related to properties of Right-angled triangle.
Suppose H = Hypotenuse, P = Perpendicular, B = Base and A = Area of right angled triangle.
We have some sort of equations as :
P^2 + B^2 = H^2
P * B = 2 * A
(P+B)^2 = P^2 + B^2 + 2*P*B = H^2 + 4*A
(P+B) = sqrt(H^2 + 4*A) ----1
(P-B)^2 = P^2 + B^2 - 2*P*B = H^2 - 4*A
mod(P-B) = sqrt(H^2 - 4*A) ----2
from equation (2) we can conclude that if
H^2 < 4*A then no solution is possible.
Further from (1)+(2) and (1)-(2) we have :
P = (sqrt(H^2 + 4*A) + sqrt(H^2 - 4*A) ) / 2
B = (sqrt(H^2 + 4*A) - sqrt(H^2 - 4*A) ) / 2
Below is the implementation of above approach:
C++
// CPP program to find dimensions of
// Right angled triangle
#include <bits/stdc++.h>
using namespace std;
// function to calculate dimension
void findDimen(int H, int A)
{
// P^2+B^2 = H^2
// P*B = 2*A
// (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
// (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
// P+B = sqrt(H^2+4*A)
// |P-B| = sqrt(H^2-4*A)
if (H * H < 4 * A) {
cout << "Not Possible\n";
return;
}
// sqrt value of H^2 + 4A and H^2- 4A
double apb = sqrt(H * H + 4 * A);
double asb = sqrt(H * H - 4 * A);
// Set precision
cout.precision(2);
cout << "P = " << fixed
<< (apb - asb) / 2.0 << "\n";
cout << "B = " << (apb + asb) / 2.0;
}
// driver function
int main()
{
int H = 5;
int A = 6;
findDimen(H, A);
return 0;
}
Java
// Java program to find dimensions of
// Right angled triangle
class GFG {
// function to calculate dimension
static void findDimen(int H, int A)
{
// P^2+B^2 = H^2
// P*B = 2*A
// (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
// (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
// P+B = sqrt(H^2+4*A)
// |P-B| = sqrt(H^2-4*A)
if (H * H < 4 * A) {
System.out.println("Not Possible");
return;
}
// sqrt value of H^2 + 4A and H^2- 4A
double apb = Math.sqrt(H * H + 4 * A);
double asb = Math.sqrt(H * H - 4 * A);
System.out.println("P = " + Math.round(((apb - asb) / 2.0) * 100.0) / 100.0);
System.out.print("B = " + Math.round(((apb + asb) / 2.0) * 100.0) / 100.0);
}
// Driver function
public static void main(String[] args)
{
int H = 5;
int A = 6;
findDimen(H, A);
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python code to find dimensions
# of Right angled triangle
# importing the math package
# to use sqrt function
from math import sqrt
# function to find the dimensions
def findDimen( H, A):
# P ^ 2 + B ^ 2 = H ^ 2
# P * B = 2 * A
# (P + B)^2 = P ^ 2 + B ^ 2 + 2 * P*B = H ^ 2 + 4 * A
# (P-B)^2 = P ^ 2 + B ^ 2-2 * P*B = H ^ 2-4 * A
# P + B = sqrt(H ^ 2 + 4 * A)
# |P-B| = sqrt(H ^ 2-4 * A)
if H * H < 4 * A:
print("Not Possible")
return
# sqrt value of H ^ 2 + 4A and H ^ 2- 4A
apb = sqrt(H * H + 4 * A)
asb = sqrt(H * H - 4 * A)
# printing the dimensions
print("P = ", "%.2f" %((apb - asb) / 2.0))
print("B = ", "%.2f" %((apb + asb) / 2.0))
# driver code
H = 5 # assigning value to H
A = 6 # assigning value to A
findDimen(H, A) # calling function
# This code is contributed by "Abhishek Sharma 44"
C#
// C# program to find dimensions of
// Right angled triangle
using System;
class GFG {
// function to calculate dimension
static void findDimen(int H, int A)
{
// P^2+B^2 = H^2
// P*B = 2*A
// (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
// (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
// P+B = sqrt(H^2+4*A)
// |P-B| = sqrt(H^2-4*A)
if (H * H < 4 * A) {
Console.WriteLine("Not Possible");
return;
}
// sqrt value of H^2 + 4A and H^2- 4A
double apb = Math.Sqrt(H * H + 4 * A);
double asb = Math.Sqrt(H * H - 4 * A);
Console.WriteLine("P = " + Math.Round(
((apb - asb) / 2.0) * 100.0) / 100.0);
Console.WriteLine("B = " + Math.Round(
((apb + asb) / 2.0) * 100.0) / 100.0);
}
// Driver function
public static void Main()
{
int H = 5;
int A = 6;
findDimen(H, A);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find dimensions
// of Right angled triangle
// function to calculate dimension
function findDimen($H, $A)
{
// P^2+B^2 = H^2
// P*B = 2*A
// (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
// (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
// P+B = sqrt(H^2+4*A)
// |P-B| = sqrt(H^2-4*A)
if ($H * $H < 4 * $A)
{
echo "Not Possible\n";
return;
}
// sqrt value of H^2 + 4A and
// H^2- 4A
$apb = sqrt($H * $H + 4 * $A);
$asb = sqrt($H * $H - 4 * $A);
echo "P = " , $fixed
, ($apb - $asb) / 2.0 , "\n";
echo "B = " , ($apb + $asb) / 2.0;
}
// Driver Code
$H = 5;
$A = 6;
findDimen($H, $A);
// This code is contributed by vt_m.
?>
JavaScript
<script>
// java script program to find dimensions
// of Right angled triangle
// function to calculate dimension
function findDimen(H, A)
{
// P^2+B^2 = H^2
// P*B = 2*A
// (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
// (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
// P+B = sqrt(H^2+4*A)
// |P-B| = sqrt(H^2-4*A)
if (H * H < 4 * A)
{
document.write( "Not Possible");
return;
}
// sqrt value of H^2 + 4A and
// H^2- 4A
let apb = Math.sqrt(H * H + 4 * A);
let asb = Math.sqrt(H * H - 4 * A);
document.write( "P = " +((apb - asb) / 2.0).toFixed(2), "<br>");
document.write( "B = " +((apb + asb) / 2.0).toFixed(2));
}
// Driver Code
let H = 5;
let A = 6;
findDimen(H, A);
// This code is contributed by Gottumukkala Bobby
</script>
Output:
P = 3.00
B = 4.00
Time complexity : O(log(n)) since using inbuilt sqrt functions
Auxiliary Space : O(1)
Similar Reads
Find other two sides and angles of a right angle triangle Given one side of right angle triangle, check if there exists a right angle triangle possible with any other two sides of the triangle. If possible print length of the other two sides and all the angles of the triangle. Examples: Input : a = 12 Output : Sides are a = 12, b = 35, c = 37 Angles are A
13 min read
Check if a right-angled triangle can be formed by the given coordinates Given three Cartesian coordinates, the task is to check if a right-angled triangle can be formed by the given coordinates. If it is possible to create a right-angled triangle, print Yes. Otherwise, print No. Examples: Input: X1=0, Y1=5, X2=19, Y2=5, X3=0, Y3=0 Output: Yes Explanation: Length of side
5 min read
Find the height of a right-angled triangle whose area is X times its base Given that the area of a right-angled triangle is X times its base b. The task is to find the height of the given triangle. Examples: Input: X = 40 Output: 80Input: X = 100 Output: 200 Approach: We know that the area of a right-angled triangle, Area = (base * height) / 2 and it is given that this ar
3 min read
Check if a right-angled triangle can be formed by moving any one of the coordinates Given three coordinates of a triangle (x1, y1), (x2, y2), (x3, y3). The task is to find out if the triangle can be transformed to a right-angled triangle by moving only one point exactly by the distance 1. If it is possible to make the triangle right-angled, then print "POSSIBLE", else print "NOT PO
12 min read
Find all sides of a right angled triangle from given hypotenuse and area | Set 1 Given hypotenuse and area of a right angle triangle, get its base and height and if any triangle with given hypotenuse and area is not possible, print not possible.Examples: Input : hypotenuse = 5, area = 6 Output : base = 3, height = 4 Input : hypotenuse = 5, area = 7 Output : No triangle possible
9 min read