Write a python program to check if a + b and c are the lengths of three sides of a triangle or not

Last update on August 19 2022 21:51:44 [UTC/GMT +8 hours]

Python Basic - 1: Exercise-34 with Solution

Write a Python program to check whether three given lengths [integers] of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No".

Input:
Integers separated by a single space.
1 ≤ length of the side ≤ 1,000

Pictorial Presentation:

Sample Solution:

Python Code:

print["Input three integers[sides of a triangle]"]
int_num = list[map[int,input[].split[]]]
x,y,z = sorted[int_num]
if x**2+y**2==z**2:
    print['Yes']
else:
    print['No']

Sample Output:

Input three integers[sides of a triangle]
 8 6 7
No

Flowchart:

Python Code Editor:

Have another way to solve this solution? Contribute your code [and comments] through Disqus.

Previous: Write a Python program to compute the digit number of sum of two given integers.
Next: Write a Python program which solve the specified equation.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Python: Tips of the Day

Union Of Collections:

To get a distinct combined set of two sets.

a = {1,2,3}
b = {3,4,5}
c = a.union[b]

  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation

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
    else:
        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.']
else:
    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.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given three sides, check whether triangle is valid or not. 
    Examples: 
     

    Input :  a = 7, b = 10, c = 5 
    Output : Valid
    
    Input : a = 1 b = 10 c = 12 
    Output : Invalid

    Approach: A triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met. 
     

    1.a + b > c 
    2.a + c > b 
    3.b + c > a  

    C++

    #include

    using namespace std;

    bool checkValidity[int a, int b, int c]

    {

        if [a + b

    Javascript

    function checkValidity[a, b, c]

    {

        if [a + b =b: return True else: 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 ...

    How do you find the area of a triangle with 3 sides in Python?

    Python program to find the area of a triangle.
    # Three sides of the triangle is a, b and c:.
    a = float[input['Enter first side: ']].
    b = float[input['Enter second side: ']].
    c = float[input['Enter third side: ']].
    # calculate the semi-perimeter..
    s = [a + b + c] / 2..
    # calculate the area..
    area = [s*[s-a]*[s-b]*[s-c]] ** 0.5..

    Chủ Đề