{"id":7809,"date":"2026-10-08T00:01:52","date_gmt":"2026-10-08T07:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7809"},"modified":"2026-10-03T13:21:50","modified_gmt":"2026-10-03T20:21:50","slug":"the-clock-angle-problem-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7809","title":{"rendered":"The Clock Angle Problem &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7796\">this month&#8217;s Exercise<\/a> is to report the angle between two hands on an analog clock. The result must be positive and it must reflect the lesser of two possible degrees.<br \/>\n<!--more--><br \/>\nI plowed through a few different solutions before I was happy with the following:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_10-Exercise.c\" rel=\"noopener\" target=\"_blank\">2026_10-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    int hour,minute,h_angle,m_angle,degrees;\r\n\r\n    <span class=\"comments\">\/* gather input *\/<\/span>\r\n    printf(\"Enter the hour: \");\r\n    scanf(\"%d\",&amp;hour);\r\n    printf(\"Enter the minute: \");\r\n    scanf(\"%d\",&amp;minute);\r\n\r\n    <span class=\"comments\">\/* confirm input *\/<\/span>\r\n    if( hour&lt;1 || hour&gt;12 )\r\n    {\r\n        printf(\"Invalid hour: %d\\n\",hour);\r\n        exit(1);\r\n    }\r\n    if( minute&lt;0 || minute&gt;59 )\r\n    {\r\n        printf(\"Invalid minute: %d\\n\",minute);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* calculate angle *\/<\/span>\r\n    h_angle = hour * (360\/12);        <span class=\"comments\">\/* 12 hours on the clock *\/<\/span>\r\n    m_angle = minute * (360\/60);    <span class=\"comments\">\/* 60 minutes on the clock *\/<\/span>\r\n    if( hour &gt; minute )\r\n        degrees = abs(m_angle-h_angle);\r\n    else\r\n        degrees = abs(h_angle-m_angle);\r\n    <span class=\"comments\">\/* ensure that the smallest angle is set *\/<\/span>\r\n    degrees = 360-degrees &lt; degrees ? 360-degrees : degrees;\r\n\r\n    <span class=\"comments\">\/* inform user *\/<\/span>\r\n    printf(\"The angle for %d:%02d is %d degrees\\n\",\r\n            hour,minute,degrees);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The first part of the code fetches standard input and tests the results. The values entered must be in range: 1 through 12 for the hour and 0 through 59 for the minute.<\/p>\n<p>The second part of the code calculates the angle. The hour angle (<code>h_angle<\/code>) is the hour value times 360 degrees divided by the 12 hours on the clock:<\/p>\n<p><code>h_angle = hour * (360\/12);<\/code><\/p>\n<p>The minute angle (<code>m_angle<\/code>) is the minute value times 360 degrees divided by the 60 minutes on a clock:<\/p>\n<p><code> m_angle = minute * (360\/60);<\/code><\/p>\n<p>The answer is the difference between the two calculated angles, <code>h_angle<\/code> and <code>m_angle<\/code>, but the result must be positive. So, for the third part of my solution a comparison is made:<\/p>\n<p><code>if( hour &gt; minute )<\/code><\/p>\n<p>The result determines whether the angle for hours is subtracted from minutes or vice-versa. In both cases, I use the <em>abs()<\/em> function to ensure that the result is positive. (The <em>abs()<\/em> function requires inclusion of the <code>math.h<\/code> header file, but in Linux you do not need to link in the math library for the function to behave.)<\/p>\n<p>Finally, to ensure that the smallest angle is always represented, a ternary statement sets the result into <em>int<\/em> variable <code>degrees<\/code>:<\/p>\n<p><code>degrees = 360-degrees &lt; degrees ? 360-degrees : degrees;<\/code><\/p>\n<p>The result is output.<\/p>\n<p>Here are a few sample runs:<\/p>\n<pre>Enter the hour: 12\r\nEnter the minute: 0\r\nThe angle for 12:00 is 0 degrees\r\n\r\nEnter the hour: 4\r\nEnter the minute: 55\r\nThe angle for 4:55 is 150 degrees\r\n\r\nEnter the hour: 6\r\nEnter the minute: 29\r\nThe angle for 6:29 is 6 degrees<\/pre>\n<p>My approach involves what appears to be a lot of redundancies. Even so, I tested the options and used various inputs. So, what I came up with, even if it&#8217;s aggressive, works. I hope that your solution met with success. As I wrote earlier, many solutions are possible for this programming puzzle. I saw a clever one on line that makes me wish I thought of it. Some solutions can be quite clever and elegant.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to report the angle between two hands on an analog clock. The result must be positive and it must reflect the lesser of two possible degrees.<\/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-7809","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\/7809","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=7809"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7809\/revisions"}],"predecessor-version":[{"id":7824,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7809\/revisions\/7824"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}