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

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
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.