Clock Angle Problem: Given time in hh:mm format, calculate the shorter angle between hour and minute hand in an analog clock.

For example,

**Input: **5:30

**Output: **15°

**Input: **9:00

**Output: **90°

**Input: **12:00

**Output: **0°

*Please note that hh:60 should be considered as (hh+1):0*

The idea is to consider the rate of change of the angle in degrees per minute. The hour hand of a 12-hour analogue clock turns 360° in 12 hours and the minute hand rotates through 360° in 60 minutes. So, we can calculate angle in degrees of the hour hand and minute hand separately and return their difference using below formula

Degree_{(hr)} = H*(360/12) + (M*360)/(12*60)

Degree_{(min)} = M*(360/60)

Here H is the hour and M is the minutes past the hour.

The angle should be in degrees and measured clockwise from the 12 o’clock position of the clock. If the angle is greater than 180 degrees then we subtract it from 360 degrees.

**C++ implementation –**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <bits/stdc++.h> using namespace std; // Function to compute the angle between hour and minute hand int findAngle(int hour, int min) { // find position of hour's hand int h = (hour * 360) / 12 + (min * 360) / (12 * 60); // find position of minute's hand int m = (min * 360) / (60); // calculate the angle difference int angle = abs(h - m); // consider shorter angle and return it if (angle > 180) angle = 360 - angle; return angle; } // Clock Angle Problem int main() { int hour = 5; int min = 30; cout << findAngle(hour, min); return 0; } |

**References:** Clock Angle Problem – Wikipedia

**Thanks for reading.**

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

What if the given time is 9:60?

Your approach will give 60 as answer, but it’s wrong.

The answer is 90.

Thanks for sharing your concerns. Please note that 9:60 is not a valid time. Input should be 10:00.

don’t overthink!