Welcome to Java Examples

Take a cup of tea and Let's Start programming

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



Example:

Input:  h = 12:00, m = 30.00
Output: 165 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.
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.
so the final answer will be the difference of the above formula...
(h*60+m)*0.5 - (h*60+m)*6
program
public class angle { public static void main(String[] args) { System.out.println(cal_angle(10,34)); } public static int cal_angle(int hour,int min) { int h_angle=(int)((hour*60+min)*0.5); int angle=(min*6)-h_angle; //now calculate minimum angle... if (angle<0) angle=-angle; return Math.min(360-angle,angle); } } output 113



0 comments :

Post a Comment