Write a Python Function to Check Whether Three Given Numbers Can Form The Sides of a Triangle

Hi Guys, welcome to my blog in this article you will learn about how to write a python function to check whether three given numbers can form the sides of a triangle

 

Title : 

Write A Python Function To Check Whether Three Given Numbers Can Form The Sides Of A Triangle


write a python function to check whether three given numbers can form the sides of a triangle


Description : 

Define a function that takes three sides of a triangle. Take an if statement and check if the sides are less than or equal to zero then return False. Also use another if statement to check if the addition of sides of a triangle is less than or equal to the other side of a triangle then also return False as the triangle cannot be formed. Once that two checks have been done return through, that means triangle can be formed


    
def check_triangle(a, b, c):
   if a <= 0 or b <= 0 or c <= 0:
       return False
   if a + b <= c or b + c <= a or c + a <= b:
       return False
   return True

    


In the main program three variables x, y and z use the input function call to ask a user to enter the sides of a triangle and then convert it into an integer.


    
x = int(input("Enter First Side of Triangle : "))
y = int(input("Enter Second Side of Triangle : "))
z = int(input("Enter Third Side of Triangle : "))


    


Once we get the triangle use the if statement to call the function that returns true or false so if true is returned then print the triangle can be formed else triangle cannot be formed.


    
if check_triangle(x, y, z):
   print("The Three Given Sides of Triangle Form a Trianlge.")
else:
   print("The Three Given Sides of Triangle Does Not Form a Trianlge.")


    




Complete Python Code : 


    
def check_triangle(a, b, c):
    if a <= 0 or b <= 0 or c <= 0:
        return False
    if a + b <= c or b + c <= a or c + a <= b:
        return False
    return True


x = int(input("Enter First Side of Triangle : "))
y = int(input("Enter Second Side of Triangle : "))
z = int(input("Enter Third Side of Triangle : "))

if check_triangle(x, y, z):
    print("The Three Given Sides of Triangle Form a Trianlge.")
else:
    print("The Three Given Sides of Triangle Does Not Form a Trianlge.")

    

 

 

Output : 


    
Enter First Side of Triangle : 1
Enter Second Side of Triangle : 2
Enter Third Side of Triangle : 3
The Three Given Sides of Triangle Does Not Form a Trianlge.


    

 

Output : 


    
Enter First Side of Triangle : 4
Enter Second Side of Triangle : 5
Enter Third Side of Triangle : 6
The Three Given Sides of Triangle Form a Trianlge.

    



Conclusion :

Above Python program uses the comparison operation, it compares the addition of two sides less than the third side of a triangle are not, if the summation of two sides is greater than the third side then it forms a triangle. Also the above Python program accepts sides of a triangle as input from the user using the input function call and then calls the function. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments