Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Take the 3 sides of the triangle as input from the user. Write a program to check whether the triangle is Equilateral, Isosceles or Scalene. If the triangle is Equilateral then print the area of triangle, If Isosceles then print the distinct side and if Scalene then print the maximum side value.

What I have tried:

a = float(input())
b = float(input())
c = float(input())

if a == b and b== c:
print ("The triangle is Equilateral triangle and area is", a**2*3**1/2 /4)
elif a == b or b==c or c==a:
print("The triangle is isoceles triangle", c)
elif a != b != c and c > a > b:
print("The triangle is scalene triangle and maximum side value is",c)


This code I have written but it is bounded for example scalene can be a triangle with "a" as maximum length side but in the above code it is bounded to "c" to be the maximum length side
Posted
Updated 18-Apr-22 22:39pm
Comments
study lover 19-Apr-22 4:04am    
and also in isosceles triangle a or b can also be distinct but I have shown only c to be distinct.
Richard MacCutchan 19-Apr-22 4:21am    
You need to test for all the different combinations. And in the case of the scalene you need to find the largest side, which means the maximum value of a,b and c. Your code will also work better if you use int types for the sides rather than floats.

A Scalene triangle has dissimilar sides: not two can be the same.
So if a == b or a == c or b == c it is not Scalene.
So if it isn't Equilateral or Isosceles it is Scalene - and you don't need to check at all.

All you need to do is find the biggest - which is trivial: if a > b and a > c it's a. Otherwise, if b > c it's b, otherwise it's c.
 
Share this answer
 
In order to generalize, you might use a list and sort it:
Python
import math
side = []
side.append(float(input()))
side.append(float(input()))
side.append(float(input()))
side.sort()
count = 0
if side[0]==side[1]:
  count = count + 1
if side[1]==side[2]:
  count = count + 1

if count == 2:
  area = side[0]**2*math.sqrt(3)/4
  print("equilateral, area = ", area)
elif count == 1:
  distinct = side[0] if side[1]==side[2] else side[2]
  print("isoscel, distinct side = ", distinct)
else:
  print("scalene, max side = ", side[2])
 
Share this answer
 
Comments
study lover 19-Apr-22 6:45am    
How to limit the value of the area of equilateral triangle to 2 decimal places ?
CPallini 19-Apr-22 6:55am    
I wonder if someone else has had the same problem...
https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900