Python Programming – John Zelle: Chapter 3 ex. 9

Write a program to calculate the area of a triangle given the length of its three sides a, b, c.

Chapter 3 Exercise 9

Python

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #Chapter 3 Exercise 9 # Write a program to calculate the area of a triangle given the length of its three sides a, b, c. from math import * def main(): print("This program to calculate the area of a triangle.") a, b, c = eval(input("Enter 3 lengths of a triangle separated by commas. ")) s = (a + b + c)/2 area = sqrt(s*(s-a)*(s-b)*(s-c)) print("The area of the triangle is ", area) main() |

Run =

This program to calculate the area of a triangle.

Enter 3 lengths of a triangle separated by commas. 4,5,7

The area of the triangle is 9.797958971132712