🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Calculating the area of a triangle is a common task in geometry. In Python, this can be done easily by applying Heron's formula, which allows us to compute the area if we know the lengths of all three sides of the triangle.
Heron's formula states that the area of a triangle with sides of length a, b, and c is sqrt(s*(s-a)*(s-b)*(s-c)), where s is the semi-perimeter of the triangle, calculated as (a+b+c)/2.
2. Program Steps
1. Define the lengths of the three sides of the triangle.
2. Calculate the semi-perimeter of the triangle.
3. Apply Heron's formula to calculate the area.
4. Print the area of the triangle.
3. Code Program
import math
# Function to calculate the area of a triangle using Heron's formula
def triangle_area(a, b, c):
# Calculate the semi-perimeter
s = (a + b + c) / 2
# Calculate the area
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
return area
# Lengths of the sides of the triangle
side_a = 5
side_b = 6
side_c = 7
# Calculate the area
area = triangle_area(side_a, side_b, side_c)
# Print the area
print(f"The area of the triangle is: {area}")
Output:
The area of the triangle is: 14.696938456699069
Explanation:
1. The math module is imported to use the sqrt function for calculating the square root.
2. The function triangle_area is defined to calculate the area of a triangle given the sides a, b, and c.
3. s is computed as the semi-perimeter of the triangle.
4. area is calculated using Heron's formula inside the math.sqrt function.
5. The lengths of the sides of the triangle side_a, side_b, and side_c are defined.
6. The triangle_area function is called with these side lengths to calculate the area.
7. The calculated area is printed, which in this case is approximately 14.697 for the sides of lengths 5, 6, and 7.
8. The f-string is used to format the output, clearly indicating the result.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment