VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-third-side-triangle-using-law-cosines/

⇱ Program to find third side of triangle using law of cosines - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find third side of triangle using law of cosines

Last Updated : 24 Apr, 2023

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


 

👁 Image


 


Output
6.04339

Time Complexity: O(log(n)), since using inbuilt sqrt function
Auxiliary Space: O(1), as we are not using any extra space.

Comment