Last Updated: 3 Jan, 2021

Angle Between Hour Hand And Minute Hand

Easy
Asked in companies
MicrosoftSalesforceAmazon

Problem statement

Given the time in hours and minutes, you need to calculate the angle between the hour hand and the minute hand.

Note :
There can be two angles between the hour hand and minute hand, you need to print a minimum of two. Also, print the floor value of angle i.e. if the angle is 15.2, you need to print 15.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first and only line of each test case contains two integers H and M denoting the time in hours and minutes.
Output format:
For each test case, print the smaller angle between the hour hand and minute hand.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= H <= 12
0 <= M <= 59

Time Limit: 1 sec

Approaches

01 Approach

  • The idea is to take the 12:00 i.e. H = 12  and M = 0 as reference time.
  • First,  calculate the angle made by the hour hand with respect to 12:00 in H hours and M minutes. In 12 hours the hour hand moves by 360 degrees i.e. 30 degrees in 1 hour and 0.5 degrees in 1 minute. So in H hours and M minutes, the angle moved by hour hand is  0.5 * (H * 60 + M).
  • Then, calculate the angle made by minute hand with respect to 12:00 in H hours and M minutes. In 60 minutes the minute hand moves by 360 degrees i.e. 6 degrees in 1 minute. So in H hours and M minutes, the angle moved by minute hand is  6 * M.
  • The difference between the two angles is the required angle.