Python Program to Check Whether Triangle is Valid or Not if Sides are Given

In the previous article, we have discussed Python Program for Triangular Matchstick Number. Also include triangle validator in python, Python Program To Check Triangle Is Valid Or Not, Valid Triangle Python, Check If Triangle Is Valid Python, valid triangle in python, Check Triangle Python whether it is correct or not, Valid Triangle Sides especially we focus on from basic level for beginners like what is the Valid Triangle Condition, Condition For Triangle, Condition For Triangle Sides, Condition For A Valid Triangleetc.

Given three sides of a triangle, the task is to check if the given triangle is valid or not for the given 3 sides in python.

A triangle is said to be valid if the sum of its two sides is greater than the third side.

Conditions to check if the given triangle is valid or Not:

Let a, b, c  are the 3 sides of a triangle. It must satisfy the following conditions:

a + b > c

a + c > b

b + c > a

Example1:

Input:

Given First side = 5
Given Second side = 7
Given Third side = 6

Output:

Yes, the triangle is valid for the given three sides

Example2:

Input:

Given First side = 4
Given Second side = 9
Given Third side = 2

Output:

The triangle is invalid for the given three sides

Program to Check Whether Triangle is Valid or Not if Sides are Given in Python

Below are the ways to check if the given triangle is valid or not for the given 3 sides in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the first side of a triangle as static input and store it in a variable.
  • Give the second side as static input and store it in another variable.
  • Give the third side as static input and store it in another variable.
  • Check if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side) or (scnd_side + thrd_side <= fst_side) using the if conditional statement.
  • If it is true, then print “The triangle is invalid for the given three sides”.
  • Else print “Yes, the triangle is valid for the given three sides”.
  • The Exit of the Program.

Below is the implementation:

# Give the first side of a triangle as static input and store it in a variable.
fst_side = 5
# Give the second side as static input and store it in another variable.
scnd_side = 7
# Give the third side as static input and store it in another variable.
thrd_side = 6
# Check if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side)
# or (scnd_side + thrd_side <= fst_side) using the if conditional statement.
if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side) or (scnd_side + thrd_side <= fst_side):
    # If it is true, then print "The triangle is invalid for the given three sides".
    print("The triangle is invalid for the given three sides")
else:
    # Else print "Yes, the triangle is valid for the given three sides"
    print("Yes, the triangle is valid for the given three sides")

Output:

Yes, the triangle is valid for the given three sides

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the first side of a triangle as user input using the int(input()) function and store it in a variable.
  • Give the second side as user input using the int(input()) function and store it in another variable.
  • Give the third side as user input using the int(input()) function and store it in another variable.
  • Check if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side) or (scnd_side + thrd_side <= fst_side) using the if conditional statement.
  • If it is true, then print “The triangle is invalid for the given three sides”.
  • Else print “Yes, the triangle is valid for the given three sides”.
  • The Exit of the Program.

Below is the implementation:

# Give the first side of a triangle as user input using the int(input()) function and
# store it in a variable.
fst_side = int(input("Enter some random number = "))
# Give the second side as user input using the int(input()) function and
# store it in another variable.
scnd_side = int(input("Enter some random number = "))
# Give the third side as user input using the int(input()) function and
# store it in another variable.
thrd_side = int(input("Enter some random number = "))
# Check if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side)
# or (scnd_side + thrd_side <= fst_side) using the if conditional statement.
if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side) or (scnd_side + thrd_side <= fst_side):
    # If it is true, then print "The triangle is invalid for the given three sides".
    print("The triangle is invalid for the given three sides")
else:
    # Else print "Yes, the triangle is valid for the given three sides"
    print("Yes, the triangle is valid for the given three sides")

Output:

Enter some random number = 4
Enter some random number = 9
Enter some random number = 2
The triangle is invalid for the given three sides

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.

Recommended Reading On: Python Program to Solve Triangular Matchstick Number

Practice these:

  1. Write A Python Program To Input All Sides Of A Triangle And Check Whether Triangle Is Valid Or Not.
  2. Write A Program To Accept Three Sides Of A Triangle As The Input And Print Whether The Triangle Is Valid Or Not. (A Triangle Is Valid If The Sum Of Any Two Sides Is Greater Than The Third Side.)
  3. Write A Program To Accept Three Sides Of A Triangle As The Input And Print Whether The Triangle Is Valid Or Not.
  4. Write A Python Program To Check A Triangle Is Valid Or Not
  5. How To Check Triangle Is Valid Or Not
  6. How To Check If A Triangle Is Valid
  7. How To Check A Triangle Is Valid Or Not
  8. How To Check Valid Triangle
  9. Write A Python Function To Check Whether Three Given Numbers Can Form The Sides Of A Triangle.
  10. How To Check Whether A Triangle Is Valid Or Not

Related Posts: