-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngleBetweenHourAndMin.java
37 lines (31 loc) · 1014 Bytes
/
AngleBetweenHourAndMin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Java program to find angle between hour and minute hands
import java.io.*;
class AngleBetweenHourAndMin
{
// Function to calculate the angle
static int calcAngle(double h, double m)
{
// validate the input
if (h <0 || m < 0 || h >12 || m > 60)
System.out.println("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);
// smaller angle of two possible angles
angle = Math.min(360-angle, angle);
return angle;
}
// Driver program
public static void main (String[] args)
{
System.out.println(calcAngle(9, 60)+" degree");
System.out.println(calcAngle(3, 30)+" degree");
}
}