Find the type of triangle with given sides in Python

In this article, we will learn how to find the type of triangle with given sides in Python. Let a, b, c represent the sides of the triangle.

Examples

Input: a = 5, b = 4, c = 3
Output: Right-angled triangle

Input: a = 3, b = 3, c = 3
Output: Acute-angled triangle

In general, if

c² = a² + b² then the triangle is a right-angled triangle.

c² < a² + b² then the triangle is an acute-angle triangle.

c² > a² + b² then the triangle is an obtuse-angle triangle.

Type of triangle in Python

1. Get the sides of the triangle from the user.

2. Calculate the square of each side of the triangle.

3. Check if sa == sc + sb or sb == sa+sc or sc == sa+sb then print Right-angled triangle.

4. If sa > sc + sb or sb > sa+sc or sc > sa+sb then print Obtuse-angled triangle.

5. Else, print Acute-angled triangle.

def triangleType(a, b, c):
    sa = pow(a, 2)
    sb = pow(b, 2)
    sc = pow(c, 2)
    if (sa == sc + sb or sb == sa+sc or sc == sa+sb):
        print("Right-angled triangle")
    elif (sa > sc + sb or sb > sa+sc or sc > sa+sb):
        print("Obtuse-angled triangle")
    else:
        print("Acute-angled triangle")
        
a = int(input("Enter side a: "))
b = int(input("Enter side b: "))
c = int(input("Enter side c: "))
triangleType(a, b, c)

Output

Enter side a: 10
Enter side b: 6
Enter side c: 8
Right-angled triangle

Enter side a: 5
Enter side b: 7
Enter side c: 8
Acute-angled triangle

Enter side a: 12
Enter side b: 7
Enter side c: 8
Obtuse-angled triangle

Also,  read

Leave a Reply

Your email address will not be published. Required fields are marked *