How to find angle of a triangle with 2 sides

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. 
 

How to find angle of a triangle with 2 sides

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*37
Input : 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:

   

How to find angle of a triangle with 2 sides

Below is the implementation of the above approach: 
 

C++

#include <bits/stdc++.h>

#include <cmath>

using namespace std;

#define PI 3.1415926535

double findAnglesA(double a, double b, double c)

{

    double A = acos((b * b + c * c - a * a) / (2 * b * c));

    return A * 180 / PI;

}

double findAnglesB(double a, double b, double c)

{

    double B = acos((a * a + c * c - b * b) / (2 * a * c));

    return B * 180 / PI;

}

void printAngles(int a, int b, int c)

{

    double x = (double)a;

    double y = (double)b;

    double z = (double)c;

    double A = findAnglesA(x, y, z);

    double B = findAnglesB(x, y, z);

    cout << "Angles are A = " << A << ", B = " <<

                        B << ", C = " << 90 << endl;

}

void printOtherSides(int n)

{  

    int b,c;

    if (n & 1)

    {

        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

    {

        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;

        }

    }

    printAngles(n,b,c);

}

int main()

{

    int a = 12;

    printOtherSides(a);

    return 0;

}

Java

import java.io.*;

class GFG {

static double  PI = 3.1415926535;

static double findAnglesA(double a, double b, double c)

{

    double A = Math.acos((b * b + c * c - a * a) / (2 * b * c));

    return A * 180 / PI;

}

static double findAnglesB(double a, double b, double c)

{

    double B = Math.acos((a * a + c * c - b * b) / (2 * a * c));

    return B * 180 / PI;

}

static void printAngles(int a, int b, int c)

{

    double x = (double)a;

    double y = (double)b;

    double z = (double)c;

    double A = findAnglesA(x, y, z);

    double B = findAnglesB(x, y, z);

    System.out.println( "Angles are A = " + A + ", B = " +

                        B + ", C = " + 90);

}

static void printOtherSides(int n)

{

    int b=0,c=0;

    if ((n & 1)>0)

    {

        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

    {

        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);

        }

    }

    printAngles(n,b,c);

}

    public static void main (String[] args) {

    int a = 12;

    printOtherSides(a);

    }

}

Python 3

import math

PI = 3.1415926535

def findAnglesA( a, b, c):

    A = math.acos((b * b + c * c - a * a) /

                              (2 * b * c))

    return A * 180 / PI

def findAnglesB(a, b, c):

    B = math.acos((a * a + c * c - b * b) /

                              (2 * a * c))

    return B * 180 / PI

def printAngles(a, b, c):

    x = a

    y = b

    z = c

    A = findAnglesA(x, y, z)

    B = findAnglesB(x, y, z)

    print("Angles are A = ", A,

          ", B = ", B , ", C = ", "90 ")

def printOtherSides(n):

    if (n & 1) :

        if (n == 1):

            print("-1")

        else:

            b = (n * n - 1) // 2

            c = (n * n + 1) // 2

            print("Side b = ", b,

                  " Side c = ", c)

    else:

        if (n == 2) :

            print("-1")

        else:

            b = n * n // 4 - 1;

            c = n * n // 4 + 1;

            print("Side b = " , b,

                  ", Side c = " , c)

    printAngles(n, b, c)

if __name__ == "__main__":

    a = 12

    printOtherSides(a)

C#

using System;

class GFG

{

static double PI = 3.1415926535;

static double findAnglesA(double a,

                          double b, double c)

{

    double A = Math.Acos((b * b + c *

                          c - a * a) /

                         (2 * b * c));

    return A * 180 / PI;

}

static double findAnglesB(double a,

                          double b, double c)

{

    double B = Math.Acos((a * a + c *

                          c - b * b) /

                         (2 * a * c));

    return B * 180 / PI;

}

static void printAngles(int a, int b, int c)

{

    double x = (double)a;

    double y = (double)b;

    double z = (double)c;

    double A = findAnglesA(x, y, z);

    double B = findAnglesB(x, y, z);

    Console.WriteLine( "Angles are A = " +

                            A + ", B = " +

                        B + ", C = " + 90);

}

static void printOtherSides(int n)

{

    int b = 0, c = 0;

    if ((n & 1) > 0)

    {

        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

    {

        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);

        }

    }

    printAngles(n, b, c);

}

public static void Main ()

{

    int a = 12;

    printOtherSides(a);

}

}

PHP

<?php

$PI = 3.1415926535;

function findAnglesA($a, $b, $c)

{

    global $PI;

    $A = acos(($b * $b + $c *

               $c - $a * $a) / (2 * $b * $c));

    return $A * 180 / $PI;

}

function findAnglesB($a, $b, $c)

{

    global $PI;

    $B = acos(($a * $a + $c *

               $c - $b * $b) / (2 * $a * $c));

    return $B * 180 / $PI;

}

function printAngles($a, $b, $c)

{

    $x = (double)$a;

    $y = (double)$b;

    $z = (double)$c;

    $A = findAnglesA($x, $y, $z);

    $B = findAnglesB($x, $y, $z);

    echo "Angles are A = " . $A .

         ", B = " . $B . ", C = 90\n";

}

function printOtherSides($n)

{

    if ($n & 1)

    {

        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

    {

        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";

        }

    }

    printAngles($n, $b, $c);

}

$a = 12;

printOtherSides($a);

?>

Javascript

<script>

 let  PI = 3.1415926535;

function findAnglesA(a, b, c)

{

    let A = Math.acos((b * b + c * c - a * a) / (2 * b * c));

    return A * 180 / PI;

}

function findAnglesB(a, b, c)

{

    let B = Math.acos((a * a + c * c - b * b) / (2 * a * c));

    return B * 180 / PI;

}

function printAngles(a, b, c)

{

    let x = a;

    let y = b;

    let z = c;

    let A = findAnglesA(x, y, z);

    let B = findAnglesB(x, y, z);

    document.write( "Angles are A = " + A + ", B = " +

                        B + ", C = " + 90);

}

function printOtherSides(n)

{

    let b=0,c=0;

    if ((n & 1)>0)

    {

        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

    {

        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/>");

        }

    }

    printAngles(n,b,c);

}

    let a = 12;

    printOtherSides(a);

</script>

Output:

Side b = 35, Side c = 37
Angles are A = 18.9246, B = 71.0754, C = 90

Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.


How do you find the third angle of a triangle given 2 sides?

All you have to do is add up the measurements of the sides you know (30° + 90° = 120°) and subtract that number from 180°. So, 180° - 120° = 60°. The measurement of that third angle is 60°.