Given two sides A, B and angle C. Find the third side of the triangle using law of cosines.
Examples:
Input : a = 5, b = 8, c = 49 Output : 6.04339
In particular, the Law of Cosines can be used to find the length of the third side of a triangle when you know the length of two sides and the angle in between. See here to learn to how to find the value of cos.
Let us assume a, b, c are the sides of triangle where c is the side across from angle C. Then,
c^2 = a^2 + b^2 - 2*a*b*cos(c) OR c = sqrt(a^2 + b^2 - 2*a*b*cos(c))

C++
// CPP program to find third // side of triangle using // law of cosines #include <bits/stdc++.h> using namespace std; // Function to calculate cos value of angle c float cal_cos(float n) { float accuracy = 0.0001, x1, denominator, cosx, cosval; // Converting degrees to radian n = n * (3.142 / 180.0); x1 = 1; // Maps the sum along the series cosx = x1; // Holds the actual value of sin(n) cosval = cos(n); int i = 1; do { denominator = 2 * i * (2 * i - 1); x1 = -x1 * n * n / denominator; cosx = cosx + x1; i = i + 1; } while (accuracy <= fabs(cosval - cosx)); return cosx; } // Function to find third side float third_side(int a, int b, float c) { float angle = cal_cos(c); return sqrt((a * a) + (b * b) - 2 * a * b * angle); } // Driver program to check the above function int main() { float c = 49; int a = 5, b = 8; // function call cout << third_side(a, b, c); return 0; } |
Java
// Java program to find third // side of triangle using // law of cosines class GFG { // Function to calculate // cos value of angle c static float cal_cos(float n) { float accuracy = 0.0001f, x1; float denominator, cosx, cosval; // Converting degrees to radian n = n * (3.142f / 180.0f); x1 = 1; // Maps the sum along the series cosx = x1; // Holds the actual value of sin(n) cosval = (float)Math.cos(n); int i = 1; do { denominator = 2 * i * (2 * i - 1); x1 = -x1 * n * n / denominator; cosx = cosx + x1; i = i + 1; } while (accuracy <= Math.abs(cosval - cosx)); return cosx; } // Function to find third side static float third_side(int a, int b, float c) { float angle = cal_cos(c); return (float)Math.sqrt((a * a) + (b * b) - 2 * a * b * angle); } // Driver code public static void main (String[] args) { float c = 49; int a = 5, b = 8; // function call System.out.print(Math.round(third_side(a, b, c)*100000.0)/100000.0); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to find third side # of triangle using law of cosines import math as mt # Function to calculate cos # value of angle c def cal_cos(n): accuracy = 0.0001 x1, denominator, cosx, cosval = 0, 0, 0, 0 # Converting degrees to radian n = n * (3.142 / 180.0) x1 = 1 # Maps the sum along the series cosx = x1 # Holds the actual value of sin(n) cosval = mt.cos(n) i = 1 while (accuracy <= abs(cosval - cosx)): denominator = 2 * i * (2 * i - 1) x1 = -x1 * n * n / denominator cosx = cosx + x1 i = i + 1 return cosx # Function to find third side def third_side(a, b, c): angle = cal_cos(c) return mt.sqrt((a * a) + (b * b) - 2 * a * b * angle) # Driver Code c = 49a, b = 5, 8print(third_side(a, b, c)) # This code is contributed by mohit kumar |
C#
// C# program to find third // side of triangle using // law of cosines using System; class GFG { // Function to calculate // cos value of angle c static float cal_cos(float n) { float accuracy = 0.0001f, x1; float denominator, cosx, cosval; // Converting degrees to radian n = n * (3.142f / 180.0f); x1 = 1; // Maps the sum along the series cosx = x1; // Holds the actual value of sin(n) cosval = (float)Math.Cos(n); int i = 1; do { denominator = 2 * i * (2 * i - 1); x1 = -x1 * n * n / denominator; cosx = cosx + x1; i = i + 1; } while (accuracy <= Math.Abs(cosval - cosx)); return cosx; } // Function to find third side static float third_side(int a, int b, float c) { float angle = cal_cos(c); return (float)Math.Sqrt((a * a) + (b * b) - 2 * a * b * angle); } // Driver code public static void Main () { float c = 49; int a = 5, b = 8; // function call Console.WriteLine(Math.Round(third_side(a, b, c)*100000.0)/100000.0); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find third // side of triangle using // law of cosines // Function to calculate // cos value of angle c function cal_cos( $n ) { $accuracy = 0.0001; $x1; $denominator; $cosx; $cosval; // Converting degrees // to radian $n = $n * (3.142 / 180.0); $x1 = 1; // Maps the sum // along the series $cosx = $x1; // Holds the actual // value of sin(n) $cosval = cos($n); $i = 1; do { $denominator = 2 * $i * (2 * $i - 1); $x1 = -$x1 * $n * $n / $denominator; $cosx = $cosx + $x1; $i = $i + 1; } while ($accuracy <= ($cosval - $cosx)); return $cosx; } // Function to find third side function third_side($a, $b, $c) { $angle = cal_cos($c); return sqrt(($a * $a) + ($b * $b) - 2 * $a * $b * $angle); } // Driver Code $c = 49; $a = 5; $b = 8; // function call echo third_side($a, $b, $c); // This code is contributed // by ajit ?> |
Output:
6.04339
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:
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Check if it is possible to draw a straight line with the given direction cosines
- Find coordinates of the triangle given midpoint of each side
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Program to print a Hollow Triangle inside a Triangle
- Find the first, second and third minimum elements in an array
- Sum of upper triangle and lower triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to find the side of the Octagon inscribed within the square
- Probability of getting more value in third dice throw
- Minimum swaps to make two strings equal by swapping only with third string
- Program to calculate area of a rhombus whose one side and diagonal are given
- Find the Side of the smallest Square that can contain given 4 Big Squares
- Find the side of the squares which are inclined diagonally and lined in a row
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Find the area of rhombus from given Angle and Side length
- Area of a Square | Using Side, Diagonal and Perimeter
- Length of diagonals of a Rhombus using length of Side and vertex Angle
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

