{"id":7189,"date":"2025-10-08T00:01:20","date_gmt":"2025-10-08T07:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7189"},"modified":"2025-10-04T12:28:12","modified_gmt":"2025-10-04T19:28:12","slug":"herons-formula-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7189","title":{"rendered":"Heron&#8217;s Formula &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7173&#038;preview=true\">this month&#8217;s Exercise<\/a> is to code Heron&#8217;s Formula. This geometric magic calculates the area of a triangle given the length of each of its three sides.<br \/>\n<!--more--><br \/>\nAs a refresher, and to copy-paste from the original post, here is Heron&#8217;s Formula:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/wikimedia.org\/api\/rest_v1\/media\/math\/render\/svg\/1e8e1851a05e4cd4e764ec7dbe96c477989fcde3\" alt=\"Heron's Formula\" \/><\/p>\n<p>Semi-perimeter value <em>s<\/em> is obtained by using this formula:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/wikimedia.org\/api\/rest_v1\/media\/math\/render\/svg\/ed2c4193212526a50585182f301e85e2f1cdfde8\" alt=\"Semi perimeter calculation\" \/><\/p>\n<p>The difficult part of working with this formula is making up the lengths for the three sides, which isn&#8217;t officially part of the challenge. (Based on <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7173&#038;cpage=1#comment-815\">Chris W&#8217;s comment<\/a> in the original post, I&#8217;ll address this issue in the future.) My solution is shown below, which gathers input from the user, obtains the semi-perimeter, then applies Heron&#8217;s Formula to generate the area for the given triangle.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_10-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_10-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n    float t[3],s,area;\r\n\r\n    <span class=\"comments\">\/* gather input for the three side lengths *\/<\/span>\r\n    for( x=0; x&lt;3; x++ )\r\n    {\r\n        printf(\"Input the length of side %c: \",'A'+x);\r\n        scanf(\"%f\",&amp;t[x]);\r\n    }\r\n\r\n    <span class=\"comments\">\/* obtain the semi-perimeter *\/<\/span>\r\n    s = ( t[0] + t[1] + t[2] ) \/ 2.0;\r\n    printf(\"Semi-perimeter is %.2f\\n\",s);\r\n\r\n    <span class=\"comments\">\/* apply Heron's Formula *\/<\/span>\r\n    area = sqrt( s * (s-t[0]) * (s-t[1]) * (s-t[2]) );\r\n    printf(\"The triangle's area is %.2f units\\n\",area);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Variable <code>x<\/code> is the looping variable, which should always be an integer for precise iterations. The other variables, including array <code>t[]<\/code>, are real numbers (<em>float<\/em>) to best calculate the triangle&#8217;s area.<\/p>\n<p>I use the <em>scanf()<\/em> function in a loop to fetch the three side values from the user. No check is made to ensure that these values represent a valid triangle on a plain solid surface.<\/p>\n<p>The semi-perimeter is calculated first:<\/p>\n<p><code>s = ( t[0] + t[1] + t[2] ) \/ 2.0;<\/code><\/p>\n<p>With the value of <code>s<\/code> determined, Heron&#8217;s Formula is applied by using this expression:<\/p>\n<p><code>area = sqrt( s * (s-t[0]) * (s-t[1]) * (s-t[2]) );<\/code><\/p>\n<p>You can compare this expression with the formula written above; sides <em>a<\/em>, <em>b<\/em>, and <em>c<\/em> are represented by array elements <code>t[0]<\/code>, <code>t[1]<\/code>, and <code>t[2]<\/code>, respectively. A <em>printf()<\/em> statement outputs the result.<\/p>\n<p>If you&#8217;re using Linux, remember that you must link in the math library for the <em>sqrt()<\/em> function to do its thing. The command line switch is <code>-lm<\/code> (little L).<\/p>\n<p>Here&#8217;s a sample run with three valid triangle side lengths:<\/p>\n<p><code>Input the length of side A: 3<br \/>\nInput the length of side B: 4<br \/>\nInput the length of side C: 5<br \/>\nSemi-perimeter is 6.00<br \/>\nThe triangle's area is 6.00 units<\/code><\/p>\n<p>Here is a sample run when invalid lengths are specified:<\/p>\n<p><code>Input the length of side A: 10<br \/>\nInput the length of side B: 2<br \/>\nInput the length of side C: 40<br \/>\nSemi-perimeter is 26.00<br \/>\nThe triangle's area is -nan units<\/code><\/p>\n<p>As I wrote in the original post, the Heron&#8217;s Formula <a href=\"https:\/\/en.wikipedia.org\/wiki\/Heron%27s_formula\" target=\"_blank\">Wikipedia page<\/a> features a gizmo that let&#8217;s you see the triangle formed with the lengths specified, which helps validate the input.<\/p>\n<p>I hope that your solution met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to code Heron&#8217;s Formula. This geometric magic calculates the area of a triangle given the length of each of its three sides.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7189","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7189"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7189\/revisions"}],"predecessor-version":[{"id":7205,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7189\/revisions\/7205"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}