Find other two sides and angles of a right angle triangle - GeeksforGeeks

03 May, 2021
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, b = 35 and c = 37 form right angle triangle because 12*12 + 35*35 = 37*37Input : a = 6 Output : Sides are a = 6, b = 8, c = 10 Angles are A = 36.8699, B = 53.1301, C = 90  
 
Approach to check if triangle exists and finding Sides: 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*a + b*b = c*c
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.Approach to find Angles: First find all sides of triangle. Then Applied “SSS” rule that’s means law of cosine:
    
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to print all sides and angles of right// angle triangle given one side#include <bits/stdc++.h>#include <cmath>using namespace std; #define PI 3.1415926535 // Function to find angle A// Angle in front of side adouble findAnglesA(double a, double b, double c){    // applied cosine rule    double A = acos((b * b + c * c - a * a) / (2 * b * c));     // convert into degrees    return A * 180 / PI;} // Function to find angle B// Angle in front of side bdouble findAnglesB(double a, double b, double c){    // applied cosine rule    double B = acos((a * a + c * c - b * b) / (2 * a * c));     // convert into degrees and return    return B * 180 / PI;} // Function to print all angles// of the right angled trianglevoid printAngles(int a, int b, int c){    double x = (double)a;    double y = (double)b;    double z = (double)c;         // for calculate angle A    double A = findAnglesA(x, y, z);         // for calculate angle B    double B = findAnglesB(x, y, z);         cout << "Angles are A = " << A << ", B = " <<                        B << ", C = " << 90 << endl;} // Function to find other two sides of the// right angled trianglevoid printOtherSides(int n){      int b,c;         // if n is odd    if (n & 1)    {        // case of n = 1 handled separately        if (n == 1)            cout << -1 << endl;        else        {            b = (n*n-1)/2;            c = (n*n+1)/2;            cout << "Side b = " << b                << ", Side c = " << c << endl;        }    }    else    {        // case of n = 2 handled separately        if (n == 2)            cout << -1 << endl;        else        {            b = n*n/4-1;            c = n*n/4+1;            cout << "Side b = " << b                << ", Side c = " << c << endl;        }    }         // Print angles of the triangle    printAngles(n,b,c);} // Driver Programint main(){    int a = 12;     printOtherSides(a);         return 0;}
// Java program to print all sides and angles of right// angle triangle given one side  import java.io.*; class GFG {   static double  PI = 3.1415926535; // Function to find angle A// Angle in front of side astatic double findAnglesA(double a, double b, double c){    // applied cosine rule    double A = Math.acos((b * b + c * c - a * a) / (2 * b * c));     // convert into degrees    return A * 180 / PI;} // Function to find angle B// Angle in front of side bstatic double findAnglesB(double a, double b, double c){    // applied cosine rule    double B = Math.acos((a * a + c * c - b * b) / (2 * a * c));     // convert into degrees and return    return B * 180 / PI;} // Function to print all angles// of the right angled trianglestatic void printAngles(int a, int b, int c){    double x = (double)a;    double y = (double)b;    double z = (double)c;         // for calculate angle A    double A = findAnglesA(x, y, z);         // for calculate angle B    double B = findAnglesB(x, y, z);         System.out.println( "Angles are A = " + A + ", B = " +                        B + ", C = " + 90);} // Function to find other two sides of the// right angled trianglestatic void printOtherSides(int n){    int b=0,c=0;         // if n is odd    if ((n & 1)>0)    {        // case of n = 1 handled separately        if (n == 1)            System.out.println( -1);        else        {            b = (n*n-1)/2;            c = (n*n+1)/2;            System.out.println( "Side b = " + b                + ", Side c = " + c );        }    }    else    {        // case of n = 2 handled separately        if (n == 2)            System.out.println( -1);        else        {            b = n*n/4-1;            c = n*n/4+1;            System.out.println( "Side b = " + b                + ", Side c = " + c);        }    }         // Print angles of the triangle    printAngles(n,b,c);} // Driver Program      public static void main (String[] args) {    int a = 12;     printOtherSides(a);    }} // This code is contributed// by inder_verma..
# Python 3 program to print all# sides and angles of right# angle triangle given one sideimport math PI = 3.1415926535 # Function to find angle A# Angle in front of side adef findAnglesA( a, b, c):         # applied cosine rule    A = math.acos((b * b + c * c - a * a) /                              (2 * b * c))     # convert into degrees    return A * 180 / PI # Function to find angle B# Angle in front of side bdef findAnglesB(a, b, c):     # applied cosine rule    B = math.acos((a * a + c * c - b * b) /                              (2 * a * c))     # convert into degrees    # and return    return B * 180 / PI # Function to print all angles# of the right angled triangledef printAngles(a, b, c):     x = a    y = b    z = c         # for calculate angle A    A = findAnglesA(x, y, z)     # for calculate angle B    B = findAnglesB(x, y, z)         print("Angles are A = ", A,          ", B = ", B , ", C = ", "90 ") # Function to find other two sides# of the right angled triangledef 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("Side b = ", b,                  " Side 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("Side b = " , b,                  ", Side c = " , c)             # Print angles of the triangle    printAngles(n, b, c) # Driver Codeif __name__ == "__main__":    a = 12     printOtherSides(a) # This code is contributed# by ChitraNayal
// C# program to print all sides// and angles of right angle// triangle given one sideusing System; class GFG{static double PI = 3.1415926535; // Function to find angle A// Angle in front of side astatic double findAnglesA(double a,                          double b, double c){    // applied cosine rule    double A = Math.Acos((b * b + c *                          c - a * a) /                         (2 * b * c));     // convert into degrees    return A * 180 / PI;} // Function to find angle B// Angle in front of side bstatic double findAnglesB(double a,                          double b, double c){    // applied cosine rule    double B = Math.Acos((a * a + c *                          c - b * b) /                         (2 * a * c));     // convert into degrees and return    return B * 180 / PI;} // Function to print all angles// of the right angled trianglestatic void printAngles(int a, int b, int c){    double x = (double)a;    double y = (double)b;    double z = (double)c;         // for calculate angle A    double A = findAnglesA(x, y, z);         // for calculate angle B    double B = findAnglesB(x, y, z);         Console.WriteLine( "Angles are A = " +                            A + ", B = " +                        B + ", C = " + 90);} // Function to find other two sides// of the right angled trianglestatic void printOtherSides(int n){    int b = 0, c = 0;         // if n is odd    if ((n & 1) > 0)    {        // case of n = 1 handled separately        if (n == 1)            Console.WriteLine( -1);        else        {            b = (n * n - 1) / 2;            c = (n * n + 1) / 2;            Console.WriteLine( "Side b = " + b                           + ", Side c = " + c);        }    }    else    {        // case of n = 2 handled separately        if (n == 2)            Console.WriteLine( -1);        else        {            b = n * n / 4 - 1;            c = n * n / 4 + 1;            Console.WriteLine( "Side b = " + b +                             ", Side c = " + c);        }    }         // Print angles of the triangle    printAngles(n, b, c);} // Driver Codepublic static void Main (){    int a = 12;         printOtherSides(a);}} // This code is contributed// by inder_verma
<?php// PHP program to print all sides// and angles of right angle triangle// given one side$PI = 3.1415926535; // Function to find angle A// Angle in front of side afunction findAnglesA($a, $b, $c){    global $PI;         // applied cosine rule    $A = acos(($b * $b + $c *               $c - $a * $a) / (2 * $b * $c));     // convert into degrees    return $A * 180 / $PI;} // Function to find angle B// Angle in front of side bfunction findAnglesB($a, $b, $c){    global $PI;    // applied cosine rule    $B = acos(($a * $a + $c *               $c - $b * $b) / (2 * $a * $c));     // convert into degrees and return    return $B * 180 / $PI;} // Function to print all angles// of the right angled trianglefunction printAngles($a, $b, $c){    $x = (double)$a;    $y = (double)$b;    $z = (double)$c;         // for calculate angle A    $A = findAnglesA($x, $y, $z);         // for calculate angle B    $B = findAnglesB($x, $y, $z);         echo "Angles are A = " . $A .         ", B = " . $B . ", C = 90\n";} // Function to find other two sides// of the right angled trianglefunction printOtherSides($n){         // if n is odd    if ($n & 1)    {        // case of n = 1 handled separately        if ($n == 1)            echo "-1\n";        else        {            $b = ($n * $n - 1) / 2;            $c = ($n * $n + 1) / 2;            echo "Side b = " . $b .                 ", Side c = " . $c . "\n";        }    }    else    {        // case of n = 2 handled separately        if ($n == 2)            echo "-1\n";        else        {            $b = $n * $n / 4 - 1;            $c = $n * $n / 4 + 1;            echo "Side b = " . $b .                 ", Side c = " . $c . "\n";        }    }         // Print angles of the triangle    printAngles($n, $b, $c);} // Driver Code$a = 12; printOtherSides($a); // This code is contributed by mits?>
<script> // Javascript program to print all sides and angles of right// angle triangle given one side  let  PI = 3.1415926535; // Function to find angle A// Angle in front of side afunction findAnglesA(a, b, c){    // applied cosine rule    let A = Math.acos((b * b + c * c - a * a) / (2 * b * c));     // convert into degrees    return A * 180 / PI;} // Function to find angle B// Angle in front of side bfunction findAnglesB(a, b, c){    // applied cosine rule    let B = Math.acos((a * a + c * c - b * b) / (2 * a * c));     // convert into degrees and return    return B * 180 / PI;} // Function to print all angles// of the right angled trianglefunction printAngles(a, b, c){    let x = a;    let y = b;    let z = c;         // for calculate angle A    let A = findAnglesA(x, y, z);         // for calculate angle B    let B = findAnglesB(x, y, z);         document.write( "Angles are A = " + A + ", B = " +                        B + ", C = " + 90);} // Function to find other two sides of the// right angled trianglefunction printOtherSides(n){    let b=0,c=0;         // if n is odd    if ((n & 1)>0)    {        // case of n = 1 handled separately        if (n == 1)            document.write( -1);        else        {            b = (n*n-1)/2;            c = (n*n+1)/2;            document.write( "Side b = " + b                + ", Side c = " + c );        }    }    else    {        // case of n = 2 handled separately        if (n == 2)            document.write( -1);        else        {            b = n*n/4-1;            c = n*n/4+1;            document.write( "Side b = " + b                + ", Side c = " + c + "<br/>");        }    }         // Print angles of the triangle    printAngles(n,b,c);} // Driver Code     let a = 12;     printOtherSides(a);            </script>
Side b = 35, Side c = 37
Angles are A = 18.9246, B = 71.0754, C = 90
 
inderDuMCA
ukasp
Mithun Kumar
sanjoy_62
math
Technical Scripter 2018
triangle
Mathematical
Technical Scripter
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Program to find GCD or HCF of two numbers
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Program for Decimal to Binary Conversion
Sieve of Eratosthenes
Operators in C / C++
The Knight's tour problem | Backtracking-1