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.

Else print -1

Note: All the sides of triangle must be positive integers

**Example 1:**

Input :a = 3Output :b = 4, c = 5Explanation :a = 3, b = 4 and c = 5 form right angle triangle because 3^{2}+ 4^{2}= 9 + 16 = 25 = 5^{2}=> 3^{2}+ 4^{2}= 5^{2}

**Example 2:**

Input :a = 11Output :b = 60, c = 61Explanation :a = 11, b = 60 and c = 61 form right angle triangle because 11^{2}+ 60^{2}= 121 + 3600 = 3721 = 61^{2}=> 11^{2}+ 60^{2}= 61^{2}

To solve this problem we first observe the Pythagoras equation. If a and b are the lengths of the legs of a right triangle and c is the length of the hypotenuse, then the sum of the squares of the lengths of the legs is equal to the square of the length of the hypotenuse.

This relationship is represented by the formula:

a^{2}+ b^{2}= c^{2}

**Case 1 – a is an odd number:**Given a, find b and cc

^{2}- b^{2}= a^{2}OR c = (a^{2}+ 1)/2; b = (a^{2}- 1)/2;Above solution works only for case when a is odd, because a

^{2}+ 1 is divisible by 2 only for odd a.**Case 2 – a is an even number:**When c-b is 2 & c+b is (a^{2})/2c-b = 2 & c+b = (a

^{2})/2 Hence, c = (a^{2})/4 + 1; b = (a^{2})/4 - 1;This works when a is even.

This solution doesn’t work for case of a = 1 and a = 2 because there isn’t any right angle triangle with side 1 or 2 with all integer sides.

## C++

`// C++ program to print other two sides of right ` `// angle traingle given one side ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Finds two sides of a right angle triangle ` `// if it exist. ` `void` `printOtherSides(` `int` `n) ` `{ ` ` ` `// if n is odd ` ` ` `if` `(n & 1) ` ` ` `{ ` ` ` `// case of n = 1 handled separately ` ` ` `if` `(n == 1) ` ` ` `cout << -1 << endl; ` ` ` `else` ` ` `{ ` ` ` `int` `b = (n*n-1)/2; ` ` ` `int` `c = (n*n+1)/2; ` ` ` `cout << ` `"b = "` `<< b ` ` ` `<< ` `", c = "` `<< c << endl; ` ` ` `} ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// case of n = 2 handled separately ` ` ` `if` `(n == 2) ` ` ` `cout << -1 << endl; ` ` ` `else` ` ` `{ ` ` ` `int` `b = n*n/4-1; ` ` ` `int` `c = n*n/4+1; ` ` ` `cout << ` `"b = "` `<< b ` ` ` `<< ` `", c = "` `<< c << endl; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `a = 3; ` ` ` `printOtherSides(a); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print other two ` `// sides of right angle traingle ` `// given one side ` ` ` `class` `GFG ` `{ ` ` ` `// Finds two sides of a right angle ` ` ` `// triangle if it they exist. ` ` ` `static` `void` `printOtherSides(` `int` `n) ` ` ` `{ ` ` ` `// if n is odd ` ` ` `if` `(n % ` `2` `!= ` `0` `) ` ` ` `{ ` ` ` `// case of n = 1 handled separately ` ` ` `if` `(n == ` `1` `) ` ` ` `System.out.println(` `"-1"` `); ` ` ` `else` ` ` `{ ` ` ` `int` `b = (n * n -` `1` `) / ` `2` `; ` ` ` `int` `c = (n *n +` `1` `) / ` `2` `; ` ` ` `System.out.println(` `"b = "` `+ b + ` ` ` `", c = "` `+c); ` ` ` `} ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// case of n = 2 handled separately ` ` ` `if` `(n == ` `2` `) ` ` ` `System.out.println(` `"-1"` `); ` ` ` `else` ` ` `{ ` ` ` `int` `b = n * n / ` `4` `- ` `1` `; ` ` ` `int` `c = n * n / ` `4` `+ ` `1` `; ` ` ` `System.out.println(` `"b = "` `+ b + ` ` ` `", c = "` `+c); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `a = ` `3` `; ` ` ` `printOtherSides(a); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python

`# Python program to print other ` `# two sides of right angle ` `# triangle given one side ` ` ` `# Finds two sides of a right angle ` `# triangle if it they exist. ` `def` `printOtherSides(n): ` ` ` ` ` `# if n is odd ` ` ` `if` `(n & ` `1` `): ` ` ` ` ` `# case of n = 1 handled ` ` ` `# separately ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `print` `(` `-` `1` `) ` ` ` `else` `: ` ` ` `b ` `=` `(n ` `*` `n ` `-` `1` `) ` `/` `/` `2` ` ` `c ` `=` `(n ` `*` `n ` `+` `1` `) ` `/` `/` `2` ` ` `print` `(` `"b ="` `, b, ` `", c ="` `, c) ` ` ` `else` `: ` ` ` ` ` `# case of n = 2 handled ` ` ` `# separately ` ` ` `if` `(n ` `=` `=` `2` `): ` ` ` `print` `(` `-` `1` `) ` ` ` `else` `: ` ` ` `b ` `=` `n ` `*` `n ` `/` `/` `4` `-` `1` ` ` `c ` `=` `n ` `*` `n ` `/` `/` `4` `+` `1` ` ` `print` `(` `"b ="` `, b` `", c ="` `, c) ` ` ` `# Driver Code ` `a ` `=` `3` `printOtherSides(a) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print other two ` `// sides of right angle traingle ` `// given one side ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Finds two sides of a right angle ` ` ` `// triangle if it they exist. ` ` ` `static` `void` `printOtherSides(` `int` `n) ` ` ` `{ ` ` ` ` ` `// if n is odd ` ` ` `if` `(n % 2 != 0) ` ` ` `{ ` ` ` ` ` `// case of n = 1 handled ` ` ` `// separately ` ` ` `if` `(n == 1) ` ` ` `Console.WriteLine(` `"-1"` `); ` ` ` `else` ` ` `{ ` ` ` `int` `b = (n * n - 1) / 2; ` ` ` `int` `c = (n * n + 1) / 2; ` ` ` `Console.Write(` `"b = "` `+ b + ` ` ` `", c = "` `+ c); ` ` ` `} ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// case of n = 2 handled ` ` ` `// separately ` ` ` `if` `(n == 2) ` ` ` `Console.Write(` `"-1"` `); ` ` ` `else` ` ` `{ ` ` ` `int` `b = n * n / 4 - 1; ` ` ` `int` `c = n * n / 4 + 1; ` ` ` `Console.Write(` `"b = "` `+ b + ` ` ` `", c = "` `+ c); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `a = 3; ` ` ` `printOtherSides(a); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print other two ` `// sides of right angle traingle ` `// given one side ` ` ` `// Finds two sides of a right angle ` `// triangle if it they exist. ` `function` `printOtherSides(` `$n` `) ` `{ ` ` ` ` ` `// if n is odd ` ` ` `if` `(` `$n` `& 1) ` ` ` `{ ` ` ` `// case of n = 1 ` ` ` `// handled separately ` ` ` `if` `(` `$n` `== 1) ` ` ` `echo` `-1 ; ` ` ` `else` ` ` `{ ` ` ` `$b` `= (` `$n` `* ` `$n` `- 1) / 2; ` ` ` `$c` `= (` `$n` `* ` `$n` `+ 1) / 2; ` ` ` `echo` `"b = "` `,` `$b` `,` `", c = "` `,` `$c` `; ` ` ` `} ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// case of n = 2 ` ` ` `// handled separately ` ` ` `if` `(` `$n` `== 2) ` ` ` `echo` `-1 ; ` ` ` `else` ` ` `{ ` ` ` `$b` `= ` `$n` `* ` `$n` `/ 4 - 1; ` ` ` `$c` `= ` `$n` `* ` `$n` `/ 4 + 1; ` ` ` `echo` `"b = "` `,` `$b` `, ` `", c = "` `, ` `$c` `; ` ` ` `} ` ` ` `} ` `} ` ` ` ` ` `// Driver Code ` ` ` `$a` `= 3; ` ` ` `printOtherSides(` `$a` `); ` ` ` `return` `0; ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output:

b = 4, c = 5

This article is contributed by **Pratik Chhajer** . 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:

- Find other two sides and angles of a right angle triangle
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Find the hypotenuse of a right angled triangle with given two sides
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find area of triangle if two vectors of two adjacent sides are given
- Check whether right angled triangle is valid or not for large sides
- Program to print binary right angle triangle
- Maximum number of squares that can fit in a right angle isosceles triangle
- Area of a largest square fit in a right angle triangle
- Length of diagonal of a parallelogram using adjacent sides and angle between them
- Angle subtended by the chord when the angle subtended by another chord of same length is given
- Exterior angle of a cyclic quadrilateral when the opposite interior angle is given
- Angle between a chord and a tangent when angle in the alternate segment is given
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given
- Length of remaining two sides of a Triangle from a given side and its adjacent angles