{"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=\u201dbanner\u201d]\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[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20find%20angle%20between%20hour%20and%20minute%20hands%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20Utility%20function%20to%20find%20minimum%20of%20two%20integers%0Aint%20min(int%20x%2C%20int%20y)%20%7B%20return%20(x%20%3C%20y)%3F%20x%3A%20y%3B%20%7D%0A%20%0Aint%20calcAngle(double%20h%2C%20double%20m)%0A%7B%0A%20%20%20%20%2F%2F%20validate%20the%20input%0A%20%20%20%20if%20(h%20%3C0%20%7C%7C%20m%20%3C%200%20%7C%7C%20h%20%3E12%20%7C%7C%20m%20%3E%2060)%0A%20%20%20%20%20%20%20%20printf(%22Wrong%20input%22)%3B%0A%20%0A%20%20%20%20if%20(h%20%3D%3D%2012)%20h%20%3D%200%3B%0A%20%20%20%20if%20(m%20%3D%3D%2060)%20m%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Calculate%20the%20angles%20moved%20by%20hour%20and%20minute%20hands%0A%20%20%20%20%2F%2F%20with%20reference%20to%2012%3A00%0A%20%20%20%20int%20hour_angle%20%3D%200.5%20*%20(h*60%20%2B%20m)%3B%0A%20%20%20%20int%20minute_angle%20%3D%206*m%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20the%20difference%20between%20two%20angles%0A%20%20%20%20int%20angle%20%3D%20abs(hour_angle%20-%20minute_angle)%3B%0A%20%0A%20%20%20%20%2F%2F%20Return%20the%20smaller%20angle%20of%20two%20possible%20angles%0A%20%20%20%20angle%20%3D%20min(360-angle%2C%20angle)%3B%0A%20%0A%20%20%20%20return%20angle%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20printf(%22%25d%20%5Cn%22%2C%20calcAngle(9%2C%2060))%3B%0A%20%20%20%20printf(%22%25d%20%5Cn%22%2C%20calcAngle(3%2C%2030))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>90\r\n75<\/pre>\n[ad type=\u201dbanner\u201d]\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}]}}