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))
C++
#include <bits/stdc++.h>
using namespace std;
float cal_cos( float n)
{
float accuracy = 0.0001, x1, denominator, cosx, cosval;
n = n * (3.142 / 180.0);
x1 = 1;
cosx = x1;
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;
}
float third_side( int a, int b, float c)
{
float angle = cal_cos(c);
return sqrt ((a * a) + (b * b) - 2 * a * b * angle);
}
int main()
{
float c = 49;
int a = 5, b = 8;
cout << third_side(a, b, c);
return 0;
}
|
Java
class GFG
{
static float cal_cos( float n)
{
float accuracy = 0 .0001f, x1;
float denominator, cosx, cosval;
n = n * ( 3 .142f / 180 .0f);
x1 = 1 ;
cosx = x1;
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;
}
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);
}
public static void main (String[] args)
{
float c = 49 ;
int a = 5 , b = 8 ;
System.out.print(Math.round(third_side(a,
b, c)* 100000.0 )/ 100000.0 );
}
}
|
Python3
import math as mt
def cal_cos(n):
accuracy = 0.0001
x1, denominator, cosx, cosval = 0 , 0 , 0 , 0
n = n * ( 3.142 / 180.0 )
x1 = 1
cosx = x1
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
def third_side(a, b, c):
angle = cal_cos(c)
return mt.sqrt((a * a) +
(b * b) - 2 * a * b * angle)
c = 49
a, b = 5 , 8
print (third_side(a, b, c))
|
C#
using System;
class GFG
{
static float cal_cos( float n)
{
float accuracy = 0.0001f, x1;
float denominator, cosx, cosval;
n = n * (3.142f / 180.0f);
x1 = 1;
cosx = x1;
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;
}
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);
}
public static void Main ()
{
float c = 49;
int a = 5, b = 8;
Console.WriteLine(Math.Round(third_side(a,
b, c)*100000.0)/100000.0);
}
}
|
PHP
<?php
function cal_cos( $n )
{
$accuracy = 0.0001;
$x1 ; $denominator ;
$cosx ; $cosval ;
$n = $n * (3.142 / 180.0);
$x1 = 1;
$cosx = $x1 ;
$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 third_side( $a , $b , $c )
{
$angle = cal_cos( $c );
return sqrt(( $a * $a ) +
( $b * $b ) - 2 *
$a * $b * $angle );
}
$c = 49;
$a = 5;
$b = 8;
echo third_side( $a , $b , $c );
?>
|
Javascript
<script>
function cal_cos( n) {
let accuracy = 0.0001, x1;
let denominator, cosx, cosval;
n = n * (3.142 / 180.0);
x1 = 1;
cosx = x1;
cosval = Math.cos(n);
let 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 third_side( a, b, c) {
let angle = cal_cos(c);
return Math.sqrt((a * a) + (b * b) - 2 * a * b * angle);
}
let c = 49;
let a = 5, b = 8;
document.write(Math.round(third_side(a, b, c) * 100000.0) / 100000.0);
</script>
|
Time Complexity: O(log(n)), since using inbuilt sqrt function
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...