Python Program to Find the Type of Triangle with Given Sides

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Given the sides of the triangle, the task is to find the type of triangle with the given sides i.e print the type of triangle acute-angled, obtuse-angled, or right-angled Triangle.

Examples:

Example1:

Input:

Given first side =11
Given second side =6
Given third side =7

Output:

The Triangle with given sides 11 6 7 is Obtuse-angled triangle

Example2:

Input:

Given first side =8
Given second side =6
Given third side =10

Output:

The Triangle with given sides 8 6 10 is Right-angled triangle

Python Program to Find the Type of Triangle with Given Sides

Below are the ways to Find the Type of Triangle with Given Sides in Python.

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

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

If c² > a² + b², the triangle has an obtuse angle.

Method #1:Using If  Else Statement (Static Input)

Approach:

  • Give the three sides of the triangle as static input and store them in three separate variables.
  • Calculate the square of each side of the triangle using the ** operator and store them in three separate variables (squareside1,squareside2,squareside3).
  • Check if squareside1== squareside3+ squareside2, squareside2== squareside1+squareside3,or squareside3== squareside1+squareside2.
  • If it is true then print it as a Right-angled triangle.
  • Print Obtuse-angled triangle if squareside1 > squareside3+ squareside2, squareside2 > squareside1+squareside3, or squareside3 > squareside1+squareside2.
  • Else Print Acute-angled triangle.
  • The Exit of the Program.

Below is the Implementation:

# Give the three sides of the triangle as static input and store them in three separate variables.
side1 = 11
side2 = 6
side3 = 7
# Calculate the square of each side of the triangle using the ** operator and
# store them in three separate variables (squareside1,squareside2,squareside3).
squareside1 = side1**2
squareside2 = side2**2
squareside3 = side3**2
# Check if squareside1== squareside3+ squareside2,
# squareside2== squareside1+squareside3,or squareside3== squareside1+squareside2.
if(squareside1 == (squareside3 + squareside2) or squareside2 == (squareside1+squareside3) or squareside3 == (squareside1+squareside2)):
    # If it is true then print it as a Right-angled triangle.
    print('The Triangle with given sides', side1,
          side2, side3, 'is Right-angled triangle ')
# Print Obtuse-angled triangle if squareside1 > squareside3+ squareside2, squareside2 > squareside1+squareside3, or squareside3 > squareside1+squareside2.
elif(squareside1 > (squareside3 + squareside2) or squareside2 > (squareside1+squareside3) or squareside3 > (squareside1+squareside2)):
    print('The Triangle with given sides', side1,
          side2, side3, 'is Obtuse-angled triangle ')
else:
    # Else Print Acute-angled triangle.
    print('The Triangle with given sides', side1,
          side2, side3, 'is Acute-angled triangle ')

Output:

The Triangle with given sides 11 6 7 is Obtuse-angled triangle

Method #2:Using If  Else Statement (User Input)

Approach:

  • Give the three sides of the triangle as user input using map(),int() and split() functions.
  • Store them in three separate variables.
  • Calculate the square of each side of the triangle using the ** operator and store them in three separate variables (squareside1,squareside2,squareside3).
  • Check if squareside1== squareside3+ squareside2, squareside2== squareside1+squareside3,or squareside3== squareside1+squareside2.
  • If it is true then print it as a Right-angled triangle.
  • Print Obtuse-angled triangle if squareside1 > squareside3+ squareside2, squareside2 > squareside1+squareside3, or squareside3 > squareside1+squareside2.
  • Else Print Acute-angled triangle.
  • The Exit of the Program.

Below is the Implementation:

# Give the three sides of the triangle as user input using map(),int() and split() functions.
side1, side2, side3 = map(int, input(
    'Enter some random three sides of the triangle ').split())
# Calculate the square of each side of the triangle using the ** operator and
# store them in three separate variables (squareside1,squareside2,squareside3).
squareside1 = side1**2
squareside2 = side2**2
squareside3 = side3**2
# Check if squareside1== squareside3+ squareside2,
# squareside2== squareside1+squareside3,or squareside3== squareside1+squareside2.
if(squareside1 == (squareside3 + squareside2) or squareside2 == (squareside1+squareside3) or squareside3 == (squareside1+squareside2)):
    # If it is true then print it as a Right-angled triangle.
    print('The Triangle with given sides', side1,
          side2, side3, 'is Right-angled triangle ')
# Print Obtuse-angled triangle if squareside1 > squareside3+ squareside2, squareside2 > squareside1+squareside3, or squareside3 > squareside1+squareside2.
elif(squareside1 > (squareside3 + squareside2) or squareside2 > (squareside1+squareside3) or squareside3 > (squareside1+squareside2)):
    print('The Triangle with given sides', side1,
          side2, side3, 'is Obtuse-angled triangle ')
else:
    # Else Print Acute-angled triangle.
    print('The Triangle with given sides', side1,
          side2, side3, 'is Acute-angled triangle ')

Output:

Enter some random three sides of the triangle 8 6 10
The Triangle with given sides 8 6 10 is Right-angled triangle

Related Programs:

Leave a Comment