Possibility of Triangle with Given Angles in Python

In this tutorial, we will solve a task to check the possibility of a triangle with positive area in Python. We will take the three angles of the triangle as input. We have to see whether a triangle, with these angles, is valid or not.

To understand this code, you need to be familiar with the conditions of a possible triangle.

First, let us proceed with this, before diving deep into the problem.

Properties of Angles of a Valid Triangle

triangle, as you would all know, is a polygon with three vertices and three sides along with three external and internal angles. This polygon has a lot of properties to satisfy its validity but let us see here the ones for angles of a triangle :

  • The sum of the three interior angles has to be 180 degrees. That is, ∠a + ∠b + ∠c = 180 degrees.
  • All the values of the angles of the triangle have to be a non-zero value. If any angle is zero, the polygon will not remain a triangle.
  • The sum of two interior angles of a triangle is always greater than the third angle. That is, ∠a+∠b > ∠c  or ∠a+∠c > ∠b or ∠b+∠c > ∠a.

All these conditions have to be true for the validity of the triangle. We will be using these properties to check if the given angles can constitute a valid triangle in the given task.

Implementation of the Properties in the Task

In this task, we will use the conditions we discussed above and if the three angles fulfill the conditions of triangle validity, we will print that the triangle is valid. If not, we will print negative,i.e, the triangle is not possible. We will take the three angles as input from the user; you may also use the angle of your choice as default.

The approach is as follows :

  • First, take the three angles as input in the main code and store them in x,y,z and pass them as an argument to the function Triangle().
  • In Triangle(), run an if statement to check if the given angles have non-zero values and that the sum of the angles gives 180If both the conditions are found to be true, then proceed to check the next condition, else print “No, triangle not possible“.
  • Next, run another if statement to check the condition of whether the sum of any two angles greater than the third angle. Check this for all combinations. If any of the combinations holdthen print “Yes, triangle is valid”.
  • Else, print “No, triangle not possible“.

Python program: Possibility of Triangle with Given Angles

# function to check validity of triangle with the given angles

def Triangle(x, y, z): 
  # to check whether sum of angles are 180 and each angle
    # not equal to 0
  if(x != 0 and y != 0 and z != 0 and (x + y + z)== 180): 
    # Checking sum of 2 angles greater than the third 
    if((x + y)>= z or (x + z)>= y or (y + z)>= x): 
      print("Yes,triangle is valid")
    else: 
      print("No,triangle not possible")
  else: 
    print("No,triangle not possible")

# Main Code
x = int(input("Enter the first angle: "))
y = int(input("Enter the second angle: "))
z = int(input("Enter the third angle: "))
Triangle(x,y,z)

Conclusion

Output :

Enter the first angle: 50

Enter the second angle: 60

Enter the third angle: 70
Yes,triangle is valid

Here, we took the input for three angles as 50,60,70 and see that 50+60+70 gives us 180. Also, all the values are non-zero and 50+60 > 70. Hence, all conditions are true and print “Yes, triangle is valid“.

 

Output :

Enter the first angle: 45

Enter the second angle: 45

Enter the third angle: 80
No,triangle not possible

 

Here, we took the input of three angles as 45,45,80 and see that all the values are non-zero and 45+45>80. But, 45+45+80 is not equal to 180 degrees. Hence, one of the conditions fails, and “No, triangle not possible” is printed. 

Thank you for going through this article by sparing your valuable t hope this article was able to solve your doubts regarding this topic. Also, you can check out the related articles given below :

Leave a Reply

Your email address will not be published. Required fields are marked *