From ffd1508c3f8d8722b202a9e8ef629faad9624d2d Mon Sep 17 00:00:00 2001 From: G-Dinusha Date: Sat, 19 Oct 2024 10:32:32 +0530 Subject: [PATCH] Update areatriangle.py --- Python-programming-1/areatriangle.py | 30 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Python-programming-1/areatriangle.py b/Python-programming-1/areatriangle.py index c8e1be798..2ad6dd958 100644 --- a/Python-programming-1/areatriangle.py +++ b/Python-programming-1/areatriangle.py @@ -1,7 +1,23 @@ -# Python Program to find the area of triangle -a = float(input("Enter first side:")) -b = float(input("Enter second side:")) -c = float(input("Enter third side:")) -s = (a + b + c) / 2 -area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 -print("Area of triangle is :",area) +# Python Program to find the area of a triangle using Heron's formula + +# Function to check if three sides can form a triangle +def is_valid_triangle(a, b, c): + return (a + b > c) and (a + c > b) and (b + c > a) + +# Input for the sides of the triangle +a = float(input("Enter first side: ")) +b = float(input("Enter second side: ")) +c = float(input("Enter third side: ")) + +# Check if the sides can form a valid triangle +if is_valid_triangle(a, b, c): + # Calculate semi-perimeter + s = (a + b + c) / 2 + + # Calculate area using Heron's formula + area = (s * (s - a) * (s - b) * (s - c)) ** 0.5 + + print("Area of triangle is:", area) +else: + print("Invalid triangle sides. Please ensure that the sum of any two sides is greater than the third side.") +