How do you find the type of triangle in python?

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Conditional: Exercise - 36 with Solution

Write a Python program to check a triangle is equilateral, isosceles or scalene.
Note :
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.

Pictorial Presentation:

How do you find the type of triangle in python?

Sample Solution:

Python Code:

print("Input lengths of the triangle sides: ")
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))

if x == y == z:
	print("Equilateral triangle")
elif x==y or y==z or z==x:
	print("isosceles triangle")
else:
	print("Scalene triangle")

Sample Output:

x: 6                                                                                                          
y: 8                                                                                                          
z: 12                                                                                                         
Scalene triangle  

Flowchart :

How do you find the type of triangle in python?

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

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

Previous: Write a Python program to check a string represent an integer or not?
Next: Write a Python program that reads two integers representing a month and day and prints the season for that month and day.

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

  • Possibility of Triangle with Given Angles in Python

31. Python Program to read Three sides of a triangle and print the type of the triangle.

How does this program work?

  • In this program you are going to learn about how to find the which type of the triangle using its three sides.
  • We can solve this problem by first calculating the side length and then classifying on comparing of side lengths.
    1. if all sides are equal, triangle will be equilateral.
    2. if any two sides are equal triangle will be Isosceles otherwise it will be Scalene.
  • Now angle can be classified by Pythagoras theorem, if sum of square of two sides is equal to square of the third side, triangle will be right angle.

Here is the code

//To read three sides of a Triangle and print the type of the triangle
#Triangle three sides
a = int(input ("Enter the first side of a triangle: "))
b = int (input("Enter the second side of a triangle: "))
c = int(input ("Enter the third side of a triangle: "))
if (a*a) + (b*b) == (c*c) or (b*b) + (c*c) == (a*a) or (c*c) + (a*a) == (b*b):
print("Right angled Triangle")
elif (a == b) and (b == c):
print("Equilateral Triangle")
elif (a==b) or(b==c) or (c==a):
print("Isosceles Triangle")
elif (a!=b and b!=c and c!=a):
print("Scalene")

How do you find the type of triangle in python?

How do you identify a triangle type?

Define acute, obtuse and right triangle. In an acute triangle, all the angles of a triangle are less than 90 degrees. In an obtuse triangle, one of the angles of a triangle is greater than 90 degrees. In a right triangle, one angle measures 90 degrees and the other two angles are acute.

How do you know if a python is isosceles triangle?

isosceles triangle code in python.
print("Input lengths of the triangle sides: ").
x = int(input("x: ")).
y = int(input("y: ")).
z = int(input("z: ")).
if x == y == z:.
print("Equilateral triangle").
elif x==y or y==z or z==x:.

How do you use the triangle method in Python?

The function Triangle() takes the given points as vertices of a triangle and computes the area of triangle with the help of area. Syntax: Triangle(x, y, z). area Parameters: where x, y, z are coordinates. Return: Area of triangle.

How do you code an equilateral triangle in Python?

def triangle(n):.
for i in range(1, n+1):.
print(' ' * n, end='').
print('* '*(i)).
n -= 1..
n = int(input()).
triangle(n).