{"id":26161,"date":"2017-10-26T20:08:18","date_gmt":"2017-10-26T14:38:18","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26161"},"modified":"2017-10-26T20:08:18","modified_gmt":"2017-10-26T14:38:18","slug":"c-programming-calculate-angle-hour-hand-minute-hand","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-calculate-angle-hour-hand-minute-hand\/","title":{"rendered":"C Programming &#8211; Calculate the angle between hour hand and minute hand"},"content":{"rendered":"<p>This problem is know as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Clock_angle_problem\" target=\"_blank\" rel=\"noopener noreferrer\">Clock angle problem<\/a> where we need to find angle between hands of an analog clock at a given time.<span id=\"more-129988\"><\/span><\/p>\n<p>Examples:<\/p>\n<pre>Input:  h = 12:00, m = 30.00\r\nOutput: 165 degree\r\n\r\nInput:  h = 3.00, m = 30.00\r\nOutput: 75 degree<\/pre>\n<p>The idea is to take 12:00 (h = 12, m = 0) as a reference. Following are detailed steps.<\/p>\n<p><strong>1)<\/strong> Calculate the angle made by hour hand with respect to 12:00 in h hours and m minutes.<br \/>\n<strong>2)<\/strong> Calculate the angle made by minute hand with respect to 12:00 in h hours and m minutes.<br \/>\n<strong>3)<\/strong> The difference between two angles is the angle between two hands.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>How to calculate the two angles with respect to 12:00? <\/strong><br \/>\nThe minute hand moves 360 degree in 60 minute(or 6 degree in one minute) and hour hand moves 360 degree in 12 hours(or 0.5 degree in 1 minute). In h hours and m minutes, the minute hand would move (h*60 + m)*6 and hour hand would move (h*60 + m)*0.5.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program to find angle between hour and minute hands<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/> <br\/>\/\/ Utility function to find minimum of two integers<br\/>int min(int x, int y) { return (x &lt; y)? x: y; }<br\/> <br\/>int calcAngle(double h, double m)<br\/>{<br\/>    \/\/ validate the input<br\/>    if (h &lt;0 || m &lt; 0 || h &gt;12 || m &gt; 60)<br\/>        printf(&quot;Wrong input&quot;);<br\/> <br\/>    if (h == 12) h = 0;<br\/>    if (m == 60) m = 0;<br\/> <br\/>    \/\/ Calculate the angles moved by hour and minute hands<br\/>    \/\/ with reference to 12:00<br\/>    int hour_angle = 0.5 * (h*60 + m);<br\/>    int minute_angle = 6*m;<br\/> <br\/>    \/\/ Find the difference between two angles<br\/>    int angle = abs(hour_angle - minute_angle);<br\/> <br\/>    \/\/ Return the smaller angle of two possible angles<br\/>    angle = min(360-angle, angle);<br\/> <br\/>    return angle;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    printf(&quot;%d \\n&quot;, calcAngle(9, 60));<br\/>    printf(&quot;%d \\n&quot;, calcAngle(3, 30));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>90\r\n75<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Calculate the angle between hour hand and minute hand &#8211; Mathematical Algorithms &#8211; The minute hand moves 360 degree in 60 minute( 6 degree)<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[77414,77448,77418,77435,77437,77449,77444,77455,77433,77422,77425,77428,77431,77453,77443,77451,77457,77438,77417,77424,77413,77450,77445,77430,77447,77427,77421,77416,77423,77419,77420,76494,77454,77446,77426,77415,77456,77436,77429,77441,73588,77412,77432,77439,73555,77452,77440,73051,77442,77434],"class_list":["post-26161","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-64-bit-encoder","tag-64-decode","tag-64-encode","tag-ascii-values-for-alphabets","tag-ascii-values-of-alphabets","tag-base-decode","tag-base-decode-online","tag-base-encode-online","tag-base64","tag-base64-decode","tag-base64-decode-online","tag-base64-decrypt","tag-base64-to-image-converter-online","tag-base65-encode","tag-basic-64-encoding","tag-bit-64-decoder","tag-byte-64","tag-char-python","tag-character-meaning","tag-convert-binary-to-base64","tag-convert-binary-to-image-online","tag-decode-64","tag-decode-64-bit","tag-decode-base64","tag-decode-encode","tag-decoder-and-encoder","tag-encode-decode","tag-encode-to-decode","tag-encoding-examples","tag-how-to-start","tag-java-ascii-table","tag-java-program-to-print-pattern-of-alphabets","tag-online-64-bit-encoder","tag-online-base64-file-encoder","tag-online-decode-base64","tag-php-base64-encode-online","tag-python-alphabet","tag-python-slice-string","tag-python-string-functions","tag-python-string-methods","tag-python-string-substring","tag-string-encoding","tag-string-formatting-in-python","tag-string-functions-in-python","tag-string-in-python","tag-string-library-python","tag-string-operations-in-python","tag-string-python","tag-utf-64","tag-what-is-base64-encoding"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26161"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26161\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}