# Distance between end points of Hour and minute hand at given time

Given four integers H, M, L1, and L2, which denotes the time as hour and minutes in a Clock of 12-Hour format and L1 and L2 denotes the length of hour hand and minute hand respectively. The task is to find the distance between the endpoints of the hour and minutes hand.

Examples:

Input: H = 3, M = 30, L1 = 3, L2 = 4
Output: 4.33499
Explanation:
At 3:30, distance between end point of hour hand and minute hand is 4.33499 Input: H = 10, M = 30, L1 = 3, L2 = 4
Output: 6.47898
Explanation:
At 10:30, distance between end point of hour hand and minute hand is 6.47898

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to find the angle between the hour hand and minute hand at the given time and then the distance between the end points of the hour hand and minute hand can be computed with the help of the cosine formulae

Distance Between End Points of the Hour and minute hand = Below is the implementation of the above approach:

## C++

 // C++ implementation to find the  // distance between the end points  // of the hour and minute hand     #include  using namespace std;     // Function to find the angle between  // Hour hand and minute hand  int calcAngle(double h, double m)  {      // Validate the input      if (h < 0 || m < 0          || h > 12 || m > 60)          printf("Wrong input");         if (h == 12)          h = 0;      if (m == 60)          m = 0;         // Calculate the angles moved      // by hour and minute hands      // with reference to 12:00      int hour_angle = 0.5 * (h * 60 + m);      int minute_angle = 6 * m;         // Find the difference      // between two angles      int angle = abs(hour_angle - minute_angle);         // Return the smaller angle      // of two possible angles      angle = min(360 - angle, angle);         return angle;  }     // Function to calculate  // cos value of angle c  float cal_cos(float n)  {      float accuracy = 0.0001, x1,            denominator, cosx, cosval;         // Converting degrees to radian      n = n * (3.142 / 180.0);         x1 = 1;         // Maps the sum      // along the series      cosx = x1;         // Holds the actual      // value of sin(n)      cosval = cos(n);      int i = 1;      do {          denominator = 2 * i * (2 * i - 1);          x1 = -x1 * n * n / denominator;          cosx = cosx + x1;          i = i + 1;      } while (accuracy <= fabs(cosval - cosx));         return cosx;  }     // Function to distance between the  // endpoints of the hour and minute hand  float distanceEndpoints(      int a, int b, float c)  {      float angle = cal_cos(c);      return sqrt((a * a)                  + (b * b)                  - 2 * a * b * angle);  }     // Driver Code  int main()  {      // Time      int hour = 3;      int min = 30;         // Length of      // hour hand      int hourHand = 3;         // Length of      // minute hand      int minHand = 4;         // calling Function for      // finding angle      // between hour hand      // and minute hand      double angle = calcAngle(hour, min);         // Function for finding      // distance between      // end points of minute      // hand and hour hand      float distance = distanceEndpoints(          minHand, hourHand, angle);      cout << distance;      return 0;  }

## Java

 // Java implementation to find the  // distance between the end points  // of the hour and minute hand  class GFG{     // Function to find the angle between  // Hour hand and minute hand  static int calcAngle(double h, double m)  {             // Validate the input      if (h < 0 || m < 0 ||          h > 12 || m > 60)          System.out.printf("Wrong input");         if (h == 12)          h = 0;      if (m == 60)          m = 0;         // Calculate the angles moved      // by hour and minute hands      // with reference to 12:00      int hour_angle = (int)(0.5 * (h * 60 + m));      int minute_angle = (int)(6 * m);         // Find the difference      // between two angles      int angle = Math.abs(hour_angle -                            minute_angle);         // Return the smaller angle      // of two possible angles      angle = Math.min(360 - angle, angle);         return angle;  }     // Function to calculate  // cos value of angle c  static float cal_cos(float n)  {      float accuracy = (float) 0.0001, x1,                               denominator,                             cosx, cosval;         // Converting degrees to radian      n = (float)(n * (3.142 / 180.0));         x1 = 1;         // Maps the sum along       // the series      cosx = x1;         // Holds the actual      // value of sin(n)      cosval = (float)Math.cos(n);      int i = 1;             do     {          denominator = 2 * i * (2 * i - 1);          x1 = -x1 * n * n / denominator;          cosx = cosx + x1;          i = i + 1;      } while (accuracy <= Math.abs(cosval - cosx));         return cosx;  }     // Function to distance between the  // endpoints of the hour and minute hand  static float distanceEndpoints(int a, int b,                                      float c)  {      float angle = cal_cos(c);      return (float) Math.sqrt((a * a) +                                (b * b) -                                 2 * a * b * angle);  }     // Driver code  public static void main(String[] args)  {             // Time      int hour = 3;      int min = 30;         // Length of      // hour hand      int hourHand = 3;         // Length of      // minute hand      int minHand = 4;         // Calling Function       // for finding angle      // between hour hand      // and minute hand      double angle = calcAngle(hour, min);         // Function for finding      // distance between      // end points of minute      // hand and hour hand      float distance = distanceEndpoints(minHand,                                          hourHand,                                         (long)angle);      System.out.printf("%.5f", distance);  }  }     // This code is contributed by 29AjayKumar

## C#

 // C# implementation to find the  // distance between the end points  // of the hour and minute hand  using System;  class GFG{     // Function to find the angle between  // Hour hand and minute hand  static int calcAngle(double h, double m)  {             // Validate the input      if (h < 0 || m < 0 ||          h > 12 || m > 60)          Console.Write("Wrong input");         if (h == 12)          h = 0;      if (m == 60)          m = 0;         // Calculate the angles moved      // by hour and minute hands      // with reference to 12:00      int hour_angle = (int)(0.5 * (h * 60 + m));      int minute_angle = (int)(6 * m);         // Find the difference      // between two angles      int angle = Math.Abs(hour_angle -                          minute_angle);         // Return the smaller angle      // of two possible angles      angle = Math.Min(360 - angle, angle);         return angle;  }     // Function to calculate  // cos value of angle c  static float cal_cos(float n)  {      float accuracy = (float) 0.0001, x1,                               denominator,                             cosx, cosval;         // Converting degrees to radian      n = (float)(n * (3.142 / 180.0));         x1 = 1;         // Maps the sum along       // the series      cosx = x1;         // Holds the actual      // value of sin(n)      cosval = (float)Math.Cos(n);      int i = 1;             do     {          denominator = 2 * i * (2 * i - 1);          x1 = -x1 * n * n / denominator;          cosx = cosx + x1;          i = i + 1;      } while (accuracy <= Math.Abs(cosval - cosx));         return cosx;  }     // Function to distance between the  // endpoints of the hour and minute hand  static float distanceEndpoints(int a, int b,                                      float c)  {      float angle = cal_cos(c);      return (float) Math.Sqrt((a * a) +                                (b * b) -                             2 * a * b * angle);  }     // Driver code  public static void Main()  {             // Time      int hour = 3;      int min = 30;         // Length of      // hour hand      int hourHand = 3;         // Length of      // minute hand      int minHand = 4;         // Calling Function       // for finding angle      // between hour hand      // and minute hand      double angle = calcAngle(hour, min);         // Function for finding      // distance between      // end points of minute      // hand and hour hand      float distance = distanceEndpoints(minHand,                                         hourHand,                                    (long)angle);      Console.Write(distance);  }  }     // This code is contributed by Code_Mech

Output:

4.33499


Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : 29AjayKumar, Code_Mech