This problem is know as Clock angle problem where we need to find angle between hands of an analog clock at a given time.

Examples:

Input:  h = 12:00, m = 30.00
Output: 165 degree

Input:  h = 3.00, m = 30.00
Output: 75 degree

The idea is to take 12:00 (h = 12, m = 0) as a reference. Following are detailed steps.

1) Calculate the angle made by hour hand with respect to 12:00 in h hours and m minutes.
2) Calculate the angle made by minute hand with respect to 12:00 in h hours and m minutes.
3) The difference between two angles is the angle between two hands.

[ad type=”banner”]

How to calculate the two angles with respect to 12:00?
The 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.

[pastacode lang=”c” manual=”%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” message=”C Program” highlight=”” provider=”manual”/]

Output:

90
75
[ad type=”banner”]