Created
October 12, 2017 02:54
-
-
Save shailrshah/489db73741cf17f3da979d022dd5e717 to your computer and use it in GitHub Desktop.
Given a time in HH:MM:SS, find the angles made by the hour and minute hands, and the minutes and seconds hands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class ClockAngle { | |
// For hour | |
// 360* -> 12 hours or 12*60 minutes | |
// 0.5* -> 1 minute | |
// Angle wrt 00:00 = 0.5 * (60H + M) | |
// For Minute | |
// 360* -> 60 minutes | |
// 6* -> 1 minute | |
// Angle wrt 00:00 = 6M | |
// Angle in between hour and minute hands | |
// |0.5 * (60H + M) - 6M| | |
// If value is greater than 180, return 360-ans | |
static double getAngleBetweenHourAndMinute(int hour, int min) { | |
double ans = Math.abs(0.5 * (60 * hour + min) - (6 * min)); | |
return ans > 180 ? 360-ans : ans; | |
} | |
// For Minute | |
// 360* -> 60 minutes or 60*60 seconds | |
// 0.1* -> 1 second | |
// Angle wrt 00:00:00 = 0.1 * (60M + S) | |
// For Second | |
// 360* -> 60 seconds | |
// 6* -> 1 second | |
// Angle wrt 00:00:00 = 6S | |
// Angle in betwen minute and seconds hands | |
// |0.1 * (60M + S) - 6S| | |
// If value is greater than 180, return 360-ans | |
static double getAngleBetweenMinuteAndSecond(int min, int sec) { | |
double ans = Math.abs(0.1 * (60 * min + sec) - (6 * sec)); | |
return ans > 180 ? 360 - ans : ans; | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
String[] time = sc.next().split(":"); | |
int hour = Integer.parseInt(time[0]); | |
int min = Integer.parseInt(time[1]); | |
int sec = Integer.parseInt(time[2]); | |
System.out.printf("The angle made between the hour and minutes hand is %.2f.\n", getAngleBetweenHourAndMinute(hour, min)); | |
System.out.printf("The angle made between the minutes and seconds hand is %.2f.\n", getAngleBetweenMinuteAndSecond(min, sec)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment