0
0
PythonProgramBeginner · 2 min read

Python Program to Check if Three Sides Form Triangle

You can check if three sides form a triangle by verifying if each side is less than the sum of the other two using a < b + c and b < a + c and c < a + b in Python.
📋

Examples

Input3, 4, 5
OutputYes, the sides form a triangle.
Input1, 2, 3
OutputNo, the sides do not form a triangle.
Input5, 5, 5
OutputYes, the sides form a triangle.
🧠

How to Think About It

To check if three sides can form a triangle, think about the triangle rule: the length of any side must be less than the sum of the other two sides. If this is true for all three sides, then the sides can form a triangle.
📐

Algorithm

1
Get the lengths of the three sides.
2
Check if the first side is less than the sum of the other two sides.
3
Check if the second side is less than the sum of the other two sides.
4
Check if the third side is less than the sum of the other two sides.
5
If all these conditions are true, return that the sides form a triangle; otherwise, they do not.
💻

Code

python
def is_triangle(a, b, c):
    if a < b + c and b < a + c and c < a + b:
        return "Yes, the sides form a triangle."
    else:
        return "No, the sides do not form a triangle."

# Example usage
print(is_triangle(3, 4, 5))
print(is_triangle(1, 2, 3))
Output
Yes, the sides form a triangle. No, the sides do not form a triangle.
🔍

Dry Run

Let's trace the example with sides 3, 4, and 5 through the code.

1

Check if 3 < 4 + 5

3 < 9 is True

2

Check if 4 < 3 + 5

4 < 8 is True

3

Check if 5 < 3 + 4

5 < 7 is True

4

All conditions true

Return 'Yes, the sides form a triangle.'

ConditionResult
3 < 4 + 5True
4 < 3 + 5True
5 < 3 + 4True
💡

Why This Works

Step 1: Triangle Inequality Rule

The code uses the rule that each side must be less than the sum of the other two sides to form a triangle.

Step 2: Check All Sides

It checks this condition for all three sides using and to ensure all must be true.

Step 3: Return Result

If all conditions are true, it returns a positive message; otherwise, it returns a negative message.

🔄

Alternative Approaches

Using sorted sides
python
def is_triangle_sorted(a, b, c):
    sides = sorted([a, b, c])
    if sides[2] < sides[0] + sides[1]:
        return "Yes, the sides form a triangle."
    else:
        return "No, the sides do not form a triangle."

print(is_triangle_sorted(3, 4, 5))
print(is_triangle_sorted(1, 2, 3))
Sorting simplifies the check to one comparison but adds sorting overhead.
Using a function with input validation
python
def is_triangle_valid(a, b, c):
    if a <= 0 or b <= 0 or c <= 0:
        return "Sides must be positive."
    if a < b + c and b < a + c and c < a + b:
        return "Yes, the sides form a triangle."
    else:
        return "No, the sides do not form a triangle."

print(is_triangle_valid(3, 4, 5))
print(is_triangle_valid(-1, 2, 3))
Adds check for positive side lengths to avoid invalid input.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

The direct comparison method is fastest and simplest; sorting adds overhead but can simplify logic.

ApproachTimeSpaceBest For
Direct comparisonO(1)O(1)Simple and fast checks
Sorted sides checkO(1) but with sorting overheadO(1)Cleaner logic with sorted input
Input validation addedO(1)O(1)Safer code with input checks
💡
Always check that side lengths are positive before testing the triangle condition.
⚠️
Forgetting to check all three conditions of the triangle inequality and only checking one or two sides.