Check if a triangle of positive area is possible with the given angles
Last Updated :
29 Jun, 2022
Given three angles. The task is to check if it is possible to have a triangle of positive area with these angles. If it is possible print “YES” else print “NO”.
Examples:
Input : ang1 = 50, ang2 = 60, ang3 = 70
Output : YES
Input : ang1 = 50, ang2 = 65, ang3 = 80
Output : NO
Approach: We can form a valid triangle if the below conditions satisfies:
- The sum of the three given angles equals to 180.
- The sum of any two angles is greater than equal to the third one.
- None of the given angles is zero.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isTriangleExists( int a, int b, int c)
{
if (a != 0 && b != 0 && c != 0 && (a + b + c)== 180)
if ((a + b)>= c || (b + c)>= a || (a + c)>= b)
return "YES" ;
else
return "NO" ;
else
return "NO" ;
}
int main()
{
int a=50, b=60, c = 70;
cout << isTriangleExists(a, b, c) << endl;
return 0;
}
|
Java
class GFG
{
static String isTriangleExists( int a, int b, int c)
{
if (a != 0 && b != 0 && c != 0 && (a + b + c)== 180 )
if ((a + b)>= c || (b + c)>= a || (a + c)>= b)
return "YES" ;
else
return "NO" ;
else
return "NO" ;
}
public static void main(String[] args)
{
int a= 50 , b= 60 , c = 70 ;
System.out.println(isTriangleExists(a, b, c));
}
}
|
Python
def isTriangleExists(a, b, c):
if (a ! = 0 and b ! = 0 and c ! = 0 and (a + b + c) = = 180 ):
if ((a + b)> = c or (b + c)> = a or (a + c)> = b):
return "YES"
else :
return "NO"
else :
return "NO"
a, b, c = 50 , 60 , 70
print (isTriangleExists( 50 , 60 , 70 ))
|
C#
class GFG
{
static string isTriangleExists( int a,
int b,
int c)
{
if (a != 0 && b != 0 &&
c != 0 && (a + b + c) == 180)
if ((a + b) >= c || (b + c) >= a ||
(a + c) >= b)
return "YES" ;
else
return "NO" ;
else
return "NO" ;
}
static void Main()
{
int a = 50, b = 60, c = 70;
System.Console.WriteLine(isTriangleExists(a, b, c));
}
}
|
PHP
<?php
function isTriangleExists( $a , $b , $c )
{
if ( $a != 0 && $b != 0 &&
$c != 0 && ( $a + $b + $c ) == 180)
if (( $a + $b )>= $c ||
( $b + $c )>= $a || ( $a + $c )>= $b )
return "YES" ;
else
return "NO" ;
else
return "NO" ;
}
$a = 50;
$b = 60;
$c = 70;
echo isTriangleExists( $a , $b , $c );
?>
|
Javascript
<script>
function isTriangleExists(a , b , c)
{
if (a != 0 && b != 0 && c != 0 && (a + b + c) == 180)
if ((a + b) >= c || (b + c) >= a || (a + c) >= b)
return "YES" ;
else
return "NO" ;
else
return "NO" ;
}
var a = 50, b = 60, c = 70;
document.write(isTriangleExists(a, b, c));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...