Visit our sponsor Five Planet Juices  
Home
Microsoft Interview Process
Microsoft HR Questions
Technical Questions
Puzzles/Riddles
Resume Tips and Template
Discuss
Question to Interviewer
Interview Tips
Term Of Use
Site Feedback


Clock Hand Angles
Write an equation that calculates the angle between the minutes hand and the hours hand in a clock at any given time...Implement it. At a first look, could be difficult...But it's not...Everyone can get the result, due to this I won't post the implementation :) 8 lines is enough!

 

Comments:

The solution that gives the smaller angle between hours & mins hands
By sun4emicrosoftinterview on Wednesday, March 21, 2007 (UMST)

int getAngleBetweenClockHands(int hour,int mins){
    int baseVal = 0;
    baseVal = abs((hour * 5) - (mins)) * 6;
    baseVal = baseVal > (360-baseVal)?(360-baseVal):baseVal;
       return baseVal;
}

 

Reply to this Comment
Average Rating:

correct answer
By JeremyCAFE on Saturday, April 21, 2007 (UMST)
result = abs((hour * 30) + (min * .5) - (min * 6)) if(result > 180) result = abs(result - 360)

Reply to this Comment
Average Rating:

elegant solution
By jagatsastry on Thursday, August 30, 2007 (UMST)
int getAngle(int hour, int min) { int baseVal; return (baseval=(abs(hour*5-min)*6))<180?baseval:(360-baseval); }

Reply to this Comment
 

elegant solution
By jagatsastry on Thursday, August 30, 2007 (UMST)
int getAngle(int hour, int min)<br> {<br> int baseVal;<br> return (baseval=(abs(hour*5-min)*6))<180?baseval:(360-baseval);<br> }

Reply to this Comment
 

elegant solution
By jagatsastry on Thursday, August 30, 2007 (UMST)
int getAngle(int hour, int min)<br> {<br> int baseVal;<br> return (baseval=(abs(hour*5-min)*6))<180?baseval:(360-baseval);<br> }

Reply to this Comment
 

Angle between hour and minute hands
By srmoody on Sunday, October 07, 2007 (UMST)

I know I must have missed something, because my submission doesnt look like most of the submissions, yet my testing indicated that this works... so please feel free to point out the mistakes I have made, or why this will not work in every case. Thanks

 

Formula:

degrees = abs((hour * 5) - minutes) * 6

 

int GetDegrees(int hour, int minutes)
{
      int degrees;
      int minutesInTheHour = hour * 5;
      degrees = abs(minutesInTheHour - minutes) * 6;
 
      return degrees;
}

Reply to this Comment
 

A one-line solution
By septembersun on Tuesday, December 04, 2007 (UMST)
Found a common mistake in all the previous posts: None of them considered the case when the hours hand would be pointing at 12. Here is a solution accomodating this possibility: float getAngle(int hours, int mins) { return abs( ((hours%12)*30+float(mins)/2) - (mins*6) ); }

Reply to this Comment
 

A one-line solution
By septembersun on Tuesday, December 04, 2007 (UMST)
Found a common mistake in all the previous posts: None of them considered the case when the hours hand would be pointing at 12. Here is a solution accommodating this possibility: float getAngle(int hours, int mins) { return abs( ((hours%12)*30+float(mins)/2) - (mins*6) ); }

Reply to this Comment
 

another one
By chauhan on Sunday, January 20, 2008 (UMST)
double hour, min; hour=5; min=45; double hourAngle, minAngle; minAngle = 360/60 * min; hourAngle = 360/60 * (5*(double)hour); hourAngle += 360/(double)12 * (min/60); cout<<" minAngle: "<< minAngle<<" hourAngle: "<<hourAngle;

Reply to this Comment
 

exact approach.- taking the little diference from the hour hand advancement
By ivanr on Thursday, January 24, 2008 (UMST)
  if(hour == 12)hour=0;
  if (hour*30>= min*6)angle = (hour*30)-(min*6)+(min*(0.5));  
  else angle = (min*6)-(hour*6)-(min*(0.5));

Reply to this Comment
 

another solution
By bluerok on Monday, February 11, 2008 (UMST)
public static double getAngle(int hour, int mins) { if (hour == mins) return 0; double theta = ((double)Math.abs(hour - mins) / 12) * 360; if (theta > 180) return 360 - theta; else return theta; }

Reply to this Comment
 

Answer
By kandukondein on Sunday, February 17, 2008 (UMST)

int GetClockAngle(int Hr,int Min)

{

   int hDegree =Hr*30;

   int mDegree= Min*6;

   if(hDegree > 180) hDegree = 360 - hDegree;

   else if(mDegree > 180) mDegree = 360 - mDegree;

  int Degree = hDegree + mDegree ;

  if(Degree > 180) Degree=360 - Degree;

  return Degree;

}

Reply to this Comment
 

Corrected one
By kandukondein on Sunday, February 17, 2008 (UMST)

Oops found a bug...The Corrected code

 

   int hDegree =Hr*30;

   int mDegree= Min*6;

  int Degree = 0;

  Degree=(hDegree - mDegree);
   if(Degree < 0) Degree = -Degree;
 if(Degree>180) Degree=360-Degree;

  return Degree;

Reply to this Comment
 

Hours, Minutes
By imnotageek on Monday, August 04, 2008 (UMST)
( (12 - hours ) * 5 - ( minutes / 60 ) * 5 + minutes ) * 6

Reply to this Comment
 

Simple answer
By aravind on Thursday, October 09, 2008 (UMST)

if(hour==12)

hour=12;

 

angle=(30*hour)+(5.5*min);

 

if(angle>180)

{

angle=-1*(360-angle);

cout<<angle;

}

else

cout<<angle;

Reply to this Comment
 

Simple answer
By aravind on Thursday, October 09, 2008 (UMST)

if(hour==12)

hour=0;

 

angle=(30*hour)+(5.5*min);

 

if(angle>180)

{

angle=-1*(360-angle);

cout<<angle;

}

else

cout<<angle;

Reply to this Comment
 

Add Your Comment

New Articles
  • Phone screen from MICROSOFT Denmark for SDET
    Technical Phone screen after Escreen

  • Please post some non-tech qs for User Experience and Business Analyst job

    I am a CS Grad applied in non-tech position.Please help!


  • Combinations in a character array.
    Write a program that takes input a char array and outputs all the combinations of the characters in the character array.
    Example: consier char array {'a','b','c'}
    the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'.


  •  

    Most Popular Articles
  • Crazy Guy On The Airplane
    A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that flight.

  • What are your greatest weaknesses
    Beware - this is an eliminator question, designed to shorten the candidate list. Any admission of a weakness or fault will earn you an “A” for honesty, but an “F” for the interview.

  • Question: Tell me about yourself.
    Many candidates, unprepared for the question, skewer themselves by rambling, recapping their life story, delving into ancient work history or personal matters.

  •  

    New Posts

  • male enhancment buy viagra tramadol online
    Posted by SurojattSit on 2008年9月30日 (UMST)

  • penis enlargement viagra cheap tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)

  • penis viagra online Tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)



  •