Angle btw 2 hands in an analog clock

One of the most asked interview question now a days is to find the angle btw the hour and minute hand of a clock

In order to calculate the angle between the hour and the minute hand of an analog clock, we would require the angle of the minute and hour hand angle with respect to 12:00. 

 

Lets get into details now. Below mentioned is the Pseudo code to derive the logic  

 

¤ Identify the minute hand angle.

¤ Identify the hour hand angle.

¤ Difference between the minute and hour hand angle.

 

To arrive at these, we should first understand the fundamentals. Let us convert the units to similar stage so that we will be able to get the logic perfectly.

The hour hand takes 12 hours to complete 360° (1 hour =60 minutes ; 12 hours =720 minutes ).

Hour hand moves 0.5° per minute (360/720=0.5).

 

Similarly, the minute had completes 60° in 60 minutes.

Thereby the minute hand move 6° per minute.

 


Now, let derive at the formula to generate the angle of the hour & minute Hand:

Given the "h" hour and "m" minutes.

 

HOUR ANGLE

 

Hour Angle=(h*60+m)*0.5

 

 

 

MINUTE ANGLE

Minute Angle=m*6

 

The difference between the both will yield you the angle between the two hands :


 Now lets proceed with writing the java code based on the above inputs

 

package org.karthikk.com;

import java.util.Scanner;

import org.apache.log4j.Logger;


public class AnalogMain {
	private static final Logger logger = Logger.getLogger(AnalogMain.class);
	public static float m=0,h=0;
	public static float minuteAngle=0, hourAngle=0, angleBtwnHands=0;

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		logger.info("Calling Main Method ");
			
		try{
			Scanner in = new Scanner(System.in);
			 
			System.out.println("Enter the hour");
			h =  Float.parseFloat(in.nextLine());
			
			System.out.println("Enter the minute");
			m =  Float.parseFloat(in.nextLine());
			
			getAngleBetween(h,m);
		}catch(Exception e){
			logger.error("Error while processing values from the user : "+e);
		}
	}
	
	public static void getAngleBetween(float inputHour, float inputMinute){
		logger.info("Calling getAngleBetween(float "+inputHour+", float "+inputMinute+")");
		
		if(isValidInputs(inputHour,inputMinute)){
		    if (inputHour == 12) inputHour = 0;
		    if (inputMinute == 60) inputMinute = 0;
		    
			minuteAngle=getMinuteAngle(inputMinute); 
			hourAngle=getHourAngle(inputHour,inputMinute);
			angleBtwnHands=Math.abs(hourAngle-minuteAngle);
			
			System.out.println(angleBtwnHands);
		}else{
			System.out.println("Invalid Inputs");
		}
	}
	
	public static boolean isValidInputs(float inputHour, float inputMinute){
		logger.info("Calling validateInputs(float "+inputHour+", float "+inputMinute+")");
		boolean result=true;
		
		if (inputHour == 12 || inputMinute > 60){
			result=false;
			logger.error("Invalid Input ");
		}
		return  result;
	}
	
	public static float getMinuteAngle(float inputMinute){
		logger.info("Calling getMinuteAngle( float "+inputMinute+")");
		float outputMinuteAngle=0;
		
		outputMinuteAngle=(inputMinute*6);
		System.out.println("outputMinuteAngle :"+outputMinuteAngle);
		return outputMinuteAngle;
	}
	
	public static float getHourAngle(float inputHour,float inputMinute){
		logger.info("Calling getHourAngle(float "+inputHour+", float "+inputMinute+")");
		float outputHourAngle=0;
		outputHourAngle=(float)((inputHour*60+inputMinute)*0.5);
		System.out.println("outputHourAngle : "+outputHourAngle);
		return outputHourAngle;
	}
}

 

Output : 

 

Calling Main Method 
Enter the hour
12
Enter the minute
30
Calling getAngleBetween(float 12.0, float 30.0)
Calling validateInputs(float 12.0, float 30.0)
Calling getMinuteAngle( float 30.0)
outputMinuteAngle :180.0
Calling getHourAngle(float 0.0, float 30.0)
outputHourAngle : 15.0
165.0

 

LIVE DEMO


Print   Email