Given area and hypotenuse, the aim is to print all sides if right triangle can exist, else print -1. We need to print all sides in ascending order.

Examples:

Input : 6 5 Output : 3 4 5 Input : 10 6 Output : -1

We have discussed a solution of this problem in below post.

Find all sides of a right angled triangle from given hypotenuse and area | Set 1

In this post, a new solution with below logic is discussed.

Let the two unknown sides be a and b

Area : A = 0.5 * a * b

Hypotenuse Square : H^2 = a^2 + b^2

Substituting b, we get H^{2} = a^{2} + (4 * A^{2})/a^{2}

On re-arranging, we get the equation a^{4} – (H^{2})(a^{2}) + 4*(A^{2})

The discriminant D of this equation would be D = H^{4} – 16*(A^{2})

If D = 0, then roots are given by the linear equation formula, roots = (-b +- sqrt(D) )/2*a

these roots would be equal to the square of the sides, finding the square roots would give us the sides.

## C++

`// C++ program to check existence of ` `// right triangle. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Prints three sides of a right trianlge ` `// from given area and hypotenuse if triangle ` `// is possible, else prints -1. ` `void` `findRightAngle(` `int` `A, ` `int` `H) ` `{ ` ` ` `// Descriminant of the equation ` ` ` `long` `D = ` `pow` `(H, 4) - 16 * A * A; ` ` ` ` ` `if` `(D >= 0) ` ` ` `{ ` ` ` `// applying the linear equation ` ` ` `// formula to find both the roots ` ` ` `long` `root1 = (H * H + ` `sqrt` `(D)) / 2; ` ` ` `long` `root2 = (H * H - ` `sqrt` `(D)) / 2; ` ` ` ` ` `long` `a = ` `sqrt` `(root1); ` ` ` `long` `b = ` `sqrt` `(root2); ` ` ` ` ` `if` `(b >= a) ` ` ` `cout << a << ` `" "` `<< b << ` `" "` `<< H; ` ` ` `else` ` ` `cout << b << ` `" "` `<< a << ` `" "` `<< H; ` ` ` `} ` ` ` `else` ` ` `cout << ` `"-1"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `findRightAngle(6, 5); ` ` ` `} ` ` ` `// This code is contributed By Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check existence of ` `// right triangle. ` ` ` `class` `GFG { ` ` ` ` ` `// Prints three sides of a right trianlge ` ` ` `// from given area and hypotenuse if triangle ` ` ` `// is possible, else prints -1. ` ` ` `static` `void` `findRightAngle(` `double` `A, ` `double` `H) ` ` ` `{ ` ` ` `// Descriminant of the equation ` ` ` `double` `D = Math.pow(H, ` `4` `) - ` `16` `* A * A; ` ` ` ` ` `if` `(D >= ` `0` `) ` ` ` `{ ` ` ` `// applying the linear equation ` ` ` `// formula to find both the roots ` ` ` `double` `root1 = (H * H + Math.sqrt(D)) / ` `2` `; ` ` ` `double` `root2 = (H * H - Math.sqrt(D)) / ` `2` `; ` ` ` ` ` `double` `a = Math.sqrt(root1); ` ` ` `double` `b = Math.sqrt(root2); ` ` ` `if` `(b >= a) ` ` ` `System.out.print(a + ` `" "` `+ b + ` `" "` `+ H); ` ` ` `else` ` ` `System.out.print(b + ` `" "` `+ a + ` `" "` `+ H); ` ` ` `} ` ` ` `else` ` ` `System.out.print(` `"-1"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` `findRightAngle(` `6` `, ` `5` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python

`# Python program to check existence of ` `# right triangle. ` `from` `math ` `import` `sqrt ` ` ` `# Prints three sides of a right trianlge ` `# from given area and hypotenuse if triangle ` `# is possible, else prints -1. ` `def` `findRightAngle(A, H): ` ` ` ` ` `# Descriminant of the equation ` ` ` `D ` `=` `pow` `(H,` `4` `) ` `-` `16` `*` `A ` `*` `A ` ` ` `if` `D >` `=` `0` `: ` ` ` ` ` `# applying the linear equation ` ` ` `# formula to find both the roots ` ` ` `root1 ` `=` `(H ` `*` `H ` `+` `sqrt(D))` `/` `2` ` ` `root2 ` `=` `(H ` `*` `H ` `-` `sqrt(D))` `/` `2` ` ` ` ` `a ` `=` `sqrt(root1) ` ` ` `b ` `=` `sqrt(root2) ` ` ` `if` `b >` `=` `a: ` ` ` `print` `a, b, H ` ` ` `else` `: ` ` ` `print` `b, a, H ` ` ` `else` `: ` ` ` `print` `"-1"` ` ` `# Driver code ` `# Area is 6 and hypotenuse is 5. ` `findRightAngle(` `6` `, ` `5` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check existence of ` `// right triangle. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Prints three sides of a right trianlge ` ` ` `// from given area and hypotenuse if triangle ` ` ` `// is possible, else prints -1. ` ` ` `static` `void` `findRightAngle(` `double` `A, ` `double` `H) ` ` ` `{ ` ` ` ` ` `// Descriminant of the equation ` ` ` `double` `D = Math.Pow(H, 4) - 16 * A * A; ` ` ` ` ` `if` `(D >= 0) { ` ` ` ` ` `// applying the linear equation ` ` ` `// formula to find both the roots ` ` ` `double` `root1 = (H * H + Math.Sqrt(D)) / 2; ` ` ` `double` `root2 = (H * H - Math.Sqrt(D)) / 2; ` ` ` ` ` `double` `a = Math.Sqrt(root1); ` ` ` `double` `b = Math.Sqrt(root2); ` ` ` ` ` `if` `(b >= a) ` ` ` `Console.WriteLine(a + ` `" "` `+ b + ` `" "` `+ H); ` ` ` `else` ` ` `Console.WriteLine(b + ` `" "` `+ a + ` `" "` `+ H); ` ` ` `} ` ` ` `else` ` ` `Console.WriteLine(` `"-1"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `findRightAngle(6, 5); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check existence of ` `// right triangle. ` ` ` `// Prints three sides of a right trianlge ` `// from given area and hypotenuse if ` `// triangle is possible, else prints -1. ` `function` `findRightAngle(` `$A` `, ` `$H` `) ` `{ ` ` ` ` ` `// Descriminant of the equation ` ` ` `$D` `= pow(` `$H` `, 4) - 16 * ` `$A` `* ` `$A` `; ` ` ` ` ` `if` `(` `$D` `>= 0) ` ` ` `{ ` ` ` ` ` `// applying the linear equation ` ` ` `// formula to find both the roots ` ` ` `$root1` `= (` `$H` `* ` `$H` `+ sqrt(` `$D` `)) / 2; ` ` ` `$root2` `= (` `$H` `* ` `$H` `- sqrt(` `$D` `)) / 2; ` ` ` ` ` `$a` `= sqrt(` `$root1` `); ` ` ` `$b` `= sqrt(` `$root2` `); ` ` ` ` ` `if` `(` `$b` `>= ` `$a` `) ` ` ` `echo` `$a` `, ` `" "` `, ` `$b` `, ` `" "` `, ` `$H` `; ` ` ` `else` ` ` `echo` `$b` `, ` `" "` `, ` `$a` `, ` `" "` `, ` `$H` `; ` ` ` `} ` ` ` `else` ` ` `echo` `"-1"` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `findRightAngle(6, 5); ` ` ` `// This code is contributed By Anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

Output:

3 4 5

This article is contributed by **Harshit Agrawal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of possible pairs of Hypotenuse and Area to form right angled triangle
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Find the hypotenuse of a right angled triangle with given two sides
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Area of a largest square fit in a right angle triangle
- Area of Circumcircle of a Right Angled Triangle
- Area of Incircle of a Right Angled Triangle
- Find the height of a right-angled triangle whose area is X times its base
- Check if a right-angled triangle can be formed by the given coordinates
- Minimum height of a triangle with given base and area
- Area of triangle formed by the axes of co-ordinates and a given straight line
- Check whether right angled triangle is valid or not for large sides
- Check if a right-angled triangle can be formed by moving any one of the coordinates
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Find area of triangle if two vectors of two adjacent sides are given
- Area of a Triangle from the given lengths of medians
- Count of Right-Angled Triangle formed from given N points whose base or perpendicular are parallel to X or Y axis
- Program to calculate area and perimeter of equilateral triangle
- Find the altitude and area of an isosceles triangle
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle