Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 2 - Elementary Programming - Programming Exercises - Page 58: 2.14

Answer

Program: # Enter three points for a triangle $\mathrm{x} 1, \mathrm{y} 1, \mathrm{x} 2, \mathrm{y} 2, \mathrm{x} 3, \mathrm{y} 3=$ eval(input( "Enter three points for a triangle: ")) # Compute the length of the three sides side1 $=\left((x 1-x 2)^{*}(x 1-x 2)+(y 1-y 2)^{*}(y 1-y 2)\right)^{* *} 0.5$ $\operatorname{side} 2=\left((x 1-x 3)^{*}(x 1-x 3)+(y 1-y 3)^{*}(y 1-y 3)\right)^{\star *} 0.5$ $\operatorname{side} 3=\left((x 3-x 2)^{*}(x 3-x 2)+(y 3-y 2)^{*}(y 3-y 2)\right)^{* *} 0.5$ #Compute the area using given formula $s=(\operatorname{side} 1+\operatorname{side} 2+\operatorname{side} 3) / 2$ area $=\left(s^{*}(s-\operatorname{side} 1)^{*}(s-\operatorname{side} 2)^{*}(s-s i d e 3)\right)^{\star *} 0.5$ #Display the area of triangle print("The area of the triangle is", round(area,1) )

Work Step by Step

Step 1 of 3: Program Plan: Create a python file with name $2_{-} 14 \mathrm{PE} . \mathrm{PY}$. In the file, - Get input from user. It is the 3 points of the triangle. In fact, there will be 6 numbers where a set of numbers will represent a vertex of triangle. - Compute the length of the three sides with the given points. - Compute the area using given formula using length of side. - Display the area of triangle. Step 2 of 3: Program: # Enter three points for a triangle $\mathrm{x} 1, \mathrm{y} 1, \mathrm{x} 2, \mathrm{y} 2, \mathrm{x} 3, \mathrm{y} 3=$ eval(input( "Enter three points for a triangle: ")) # Compute the length of the three sides side1 $=\left((x 1-x 2)^{*}(x 1-x 2)+(y 1-y 2)^{*}(y 1-y 2)\right)^{* *} 0.5$ $\operatorname{side} 2=\left((x 1-x 3)^{*}(x 1-x 3)+(y 1-y 3)^{*}(y 1-y 3)\right)^{\star *} 0.5$ $\operatorname{side} 3=\left((x 3-x 2)^{*}(x 3-x 2)+(y 3-y 2)^{*}(y 3-y 2)\right)^{* *} 0.5$ #Compute the area using given formula $s=(\operatorname{side} 1+\operatorname{side} 2+\operatorname{side} 3) / 2$ area $=\left(s^{*}(s-\operatorname{side} 1)^{*}(s-\operatorname{side} 2)^{*}(s-s i d e 3)\right)^{\star *} 0.5$ #Display the area of triangle print("The area of the triangle is", round(area,1) ) Step 3 of 3: Sample Output: Enter three points for a triangle: $1.5,-3.4,4.6,5,9.5,-3.4$ The area of the triangle is $33.6$
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.