java

findAngleBetweenClockHands()

Parameters: int hour, int minute

hour represents the hour hand, minute represents the minute hand

Returns: angle in degrees between the hour and minute hand of a clock

This function takes in two integers, representing the hour and minute respectively, and returns the smaller angle between the two hands on a clock at that time.

variables
methods
mathematical operations
conditional statements
Easy dificulty

Calculating the Angle Between Clock Hands in Java

Hello there, fellow programmer! This post is designed with you in mind. We know you're looking to hone your skills, hence we're going to walk you through creating a unique function in Java, namely 'find-angle-between-clock-hands'. This function can calculate the angle between the hour and minute hand in a clock based on the given time. If you're ready, let's get into the mechanics of how you can program this function.

Step 1: Understanding the Problem

We are given the hours and the minutes of the clock and we are required to compute the angle between the hour and minute hands. The hour hand moves 0.5 degrees per minute and the minute hand moves 6 degrees per minute. By knowing this, we can calculate the absolute difference between the two hands and check this against 360 to get the smallest angle.

int hourAngle = (hour * 60 + minute) / 2;
int minuteAngle = minute * 6;

Step 2: Calculating the Absolute Difference

Next, we subtract the smaller angle from the larger angle. However, we need the minimum angle between the two hands, so if the result is more than 180 we subtract it from 360.

int diff = Math.abs(hourAngle - minuteAngle);
diff = Math.min(360 - diff, diff);

Step 3: Creating the Function

Now, we put everything together inside a function. The function will take two arguments which represent the hour and minute on a clock respectively and will give us as output the angle which is the smallest.

public static int findAngle(int hour, int minute) {
    if (hour < 0 || minute < 0 || hour > 12 || minute > 60) {
      System.out.println('Invalid Input');
      return -1;
    }

    if (hour == 12) hour = 0;
    if (minute == 60) { minute = 0; hour += 1; }

    int hourAngle = (hour * 60 + minute) * 0.5;
    int minuteAngle = minute * 6;

    int angle = Math.abs(hourAngle - minuteAngle);
    angle = Math.min(360 - angle, angle);

    return angle;
}

Conclusion

Now we have finished our findAngle function. We used the fact that the position of hour and minute hand of the clock can be expressed by a simple formula. Thus, we can find the angle between them by these positions and choose the minimum between the direct difference and the circular difference.

Learn function in:

Angle Between Clock Hands

The function calculates the smaller angle between the two hands of a clock

Learn more

Mathematical principle

The function uses modular arithmetic and the property of angles in a circle. If `h` is hours and `m` is minutes, the position of the hour hand is `0.5 * (h * 60 + m)` and the position of the minute hand is `6 * m`. Therefore, angle between hour and minute hand is `Math.abs(0.5 * (h * 60 + m) - 6 * m)`. If this angle is greater than 180°, return `360 - angle` to get the smaller angle.

Learn more