Check if a triangle of positive area is possible with the given angles in Python

PythonServer Side ProgrammingProgramming

Suppose we have three angles. We have to check whether it is possible to create a triangle of positive area with these angles or not.

So, if the input is like a = 40 b = 120 c = 20, then the output will be True as the sum of 40 + 120 + 20 = 180.

To solve this, we will follow these steps −

  • if a, b and c are not 0 and (a + b + c) is same as 180, then
    • if (a + b) >= c or (b + c) >= a or (a + c) >= b, then
      • return True
    • otherwise,
      • return False
  • otherwise,
    • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

def solve(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 True
      else:
         return False
   else:
      return False
a = 40
b = 120
c = 20
print(solve(a, b, c))

Input

40, 120, 20

Output

True
raja
Published on 29-Dec-2020 13:28:56
Advertisements