Open In App

Find other two sides of a right angle triangle

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 3
Output : b = 4, c = 5
Explanation : a = 3, b = 4 and c = 5 form right 
angle triangle because 
32 + 42 = 9 + 16 = 25 = 52 => 32 + 42 = 52

Example 2: 

Input : a = 11
Output : b = 60, c = 61
Explanation : a = 11, b = 60 and c = 61 form 
right angle triangle because
112 + 602 = 121 + 3600 = 3721 = 612 => 112 + 602 = 612

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:  

a2 + b2 = c2

 

Right angle triangle

 

  • Case 1 – a is an odd number: Given a, find b and c
c2 - b2 = a2
OR
c = (a2 + 1)/2;
b = (a2 - 1)/2;
  • Above solution works only for case when a is odd, because a2 + 1 is divisible by 2 only for odd a.
  • Case 2 – a is an even number: When c-b is 2 & c+b is (a2)/2
c-b = 2 & c+b = (a2)/2
Hence,
c = (a2)/4 + 1;
b = (a2)/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 triangle 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;
}


Java




// Java program to print other two 
// sides of right angle triangle 
// 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.


Python3




# 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


C#




// C# program to print other two 
// sides of right angle triangle 
// 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.


PHP




<?php
// PHP 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.
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.
?>


Javascript




<script>
  
// javascript 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.
function printOtherSides(n)
{
    // if n is odd
    if (n % 2 != 0)
    {
        // case of n = 1 handled separately
        if (n == 1)
            document.write("-1");
        else
        {
            var b = (n * n -1) / 2;
            var c = (n *n  +1) / 2;
            document.write("b = "+ b +
                               ", c = "+c);
        }
    }
    else
    {
        // case of n = 2 handled separately
        if (n == 2)
            document.write("-1");
        else
        {
            var b = n * n / 4 - 1;
            var c = n * n / 4 + 1;
            document.write("b = "+ b +
                               ", c = "+c);
        }
    }
      
// Driver code
var a = 3;
printOtherSides(a);
  
  
// This code is contributed by 29AjayKumar 
  
</script>


Output: 

b = 4, c = 5
 

Time complexity: O(1)
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 = 18.9246, B = 71.0754, C = 90 Explanation: a = 12
13 min read
Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
Given two integers A, B representing the length of two sides of a triangle and an integer K representing the angle between them in radian, the task is to calculate the area of the triangle from the given information.Examples: Input: a = 9, b = 12, K = 2 Output: 49.1 Explanation: Area of triangle = 1 / 2 * (9 * 12 * Sin 2) = 35.12Input: A = 2, B = 4
4 min read
Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
Given a binary matrix arr[][] of dimensions N * M , the task is to count the number of right-angled triangles that can be formed by joining the cells containing the value 1 such that the triangles must have any two of its sides parallel to the sides of the rectangle. Examples: Input: arr[][] = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}Output: 4Explanation:
8 min read
Nth angle of a Polygon whose initial angle and per angle increment is given
Given four integers N, A, K, n where N represents the number of sides the polygon, A represents the initial angle of the polygon, K represents the per angle increase, the task is to find the nth angle of the polygon having N sides. If it is not possible then print -1. Examples: Input: N = 3, A = 30, K = 30, n = 3Output: 90Explanation:The 3rd angle
6 min read
Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
Given here is a right angle triangle with height l, base b &amp; hypotenuse h, which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.Examples: Input: l = 5, b = 12, h = 13 Output: 8.77914 Input: l = 3, b = 4, h = 5 Output: 2.07116 Approach: We know, the side of
5 min read
Find the hypotenuse of a right angled triangle with given two sides
Given the other two sides of a right angled triangle, the task is to find it's hypotenuse.Examples: Input: side1 = 3, side2 = 4 Output: 5.00 32 + 42 = 52Input: side1 = 12, side2 = 15 Output: 19.21 Approach: Pythagoras theorem states that the square of hypotenuse of a right angled triangle is equal to the sum of squares of the other two sides.Below
3 min read
Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square
Given a positive integer N representing the side of a square, the task is to find the area of a triangle formed by connecting the midpoints of two adjacent sides and vertex opposite to the two sides. Examples: Input: N = 10Output: 37.5 Input: N = 1Output: 0.375 Approach: The given problem can be solved based on the following observations: The one s
5 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 with above specification. Recommended PracticeArea
9 min read
Find area of triangle if two vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a triangle. The task is to find out the area of a triangle.Examples: Input: x1 = -2, y1 = 0, z1 = -5 x2 = 1, y2 = -2, z2 = -1 Output: Area = 6.422616289332565Input: x1 = -2, y1 = 1, z1 = 5 x2 = 1, y2 = 3, z2 = -1 Output: Area = 8.860022573334675 Approach: Suppose we have two vectors a
5 min read
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle. Examples: Input: arr[] = {1, 4, 7, 4}Output: 3Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4
7 min read