Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update areatriangle.py #2972

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions Python-programming-1/areatriangle.py
Original file line number Diff line number Diff line change
@@ -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.")