Important Python Concept

Check if a Triangle of the Positive Area is Possible with the Given Angles in Python

We will learn about the python program to check if a triangle of the positive area is possible with the given angles in python. If it is possible then it should print “YES” if not it should print “NO”.

Rules

Before going to the code to check if a triangle of the positive area is possible or not there are a few important rules we should keep in mind they are,

  1. The sum of all three angles should be equal to 180.
  2. The sum of any two angles must be greater than or equal to the third angle.
  3. From the given three angles no angle should be equal to zero.

Example

Let us consider the 3 angle names as angle1, angle2, and angle3.

INPUT

angle1 = 80, angle2 = 40, and angle3 = 60

OUTPUT

YES

INPUT

angle1 = 120, angle2 = 40, and angle3 = 20

OUTPUT

NO

INPUT

angle1 = 0, angle2 = 90, and angle3 = 90

OUTPUT

NO

Program Code

def checkIfTriangleExist(angle1, angle2, angle3):
    if (angle1 != 0 and angle2 != 0 and angle3 != 0 and (angle1 + angle2 + angle3) == 180):
        if ((angle1 + angle2) >= angle3 and (angle2 + angle3) >= angle1 and (angle1
                                                                             + angle3) >= angle2):
            return "YES"
        else:
            return "NO"
    else:
        return "NO"


angle1, angle2, angle3 = 80, 40, 60
print(checkIfTriangleExist(80, 40, 60))

Output

YES

Explanation 

A method checkIfTriangleExist is created which will take the 3 angles as input. In the first, if loop we have written two conditions, one will check if any one of the given angles is equal to zero or not, and the second one will check if the sum of the given three angles is equal to 180 degrees or not if both the conditions are true the second if loop is executed else, the else block is executed and it will print NO.

In the second, if loop will check the last condition which is, the sum of any two angles must be greater than or equal to the third angle, if the condition is true then it will print YES if the condition is false else block will execute and NO will be printed.

Finally, using the print statement the values are inputted and printed.

Conclusion

In this article, we have discussed the problem statement, rules associated with the problem statement, python code for the problem statement, and an explanation for the python code.

Aniket Malik

Aniket Malik

CERTIFIED TUTOR/TRAINER WITH 300+ REVIEWS

Facing difficulty with

this concept?