Python Program to Check Validity of Triangle Given Three Sides

Table of Contents

This program checks whether given three sides of a triangle forms a valid triangle or not.

In mathematics, the triangle inequality states that for any triangle to be valid, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side.

If a, b and c are three sides of triangle then following conditions must be satisfied for a valid triangle.

a + b ≤ c

b + c ≤ a

c + a ≤ b

Python Source Code : Validity of Triangle Given Sides

# Validity of Triangle given sides

# Function definition to check validity
def is_valid_triangle(a,b,c):
    if a+b>=c and b+c>=a and c+a>=b:
        return True
        return False

# Reading Three Sides
side_a = float(input('Enter length of side a: '))
side_b = float(input('Enter length of side b: '))
side_c = float(input('Enter length of side c: '))

# Function call & making decision

if is_valid_triangle(side_a, side_b, side_c):
    print('Triangle is Valid.')
    print('Triangle is Invalid.')

Python Output: Validity of Triangle

Run 1:
Enter length of side a: 2
Enter length of side b: 2
Enter length of side c: 4
Triangle is Valid.

Run 2:
Enter length of side a: 2
Enter length of side b: 4
Enter length of side c: 15
Triangle is Invalid.