Problem of the Day
A new programming or logic puzzle every Mon-Fri

Clock Angle

Today's problem comes to us from /u/newbie12q. Thanks for the submission!

Create a function that takes in an hour and a minute input and finds the angle between the two hands. For example:

findAngle(12, 30)
> 165

Permalink: http://problemotd.com/problem/clock-angle/

Comments:

  • Anonymous - 10 years, 4 months ago

    I think you should put the angle is in degrees
    /u/newbie12q

    reply permalink

  • Johnathan - 10 years, 4 months ago

    It needs more error checking or explaining, but values like 5:15 or 05:15 both work and that's enough for me.

    def clockangle(h, m):
        hour = (60*h + m)/2
        minute = 6*m
    
        angle = abs(hour - minute)
        return angle
    
    x = raw_input('Please enter the time: ')
    
    h = float(x[:-3])
    if len(x) == 5:
        m = float(x[3:])
    elif len (x) == 4:
        m = float(x[2:])
    
    print "The angle between the two clock hands at", x, "would be", clockangle(h, m), "degrees."
    raw_input('')
    

    reply permalink

  • Anonymous - 10 years, 4 months ago

    ''' /** * ClockAngle.java * Calculates the angle between the hands of an * analogue clock given the value of the hour and * minute hands */

    import java.util.Scanner;

    public class ClockAngle{

    private static final int DEGREES = 360;
    private static final int HOURS = 12;
    private static final int MINUTES = 60;
    
    public static void main(String [] args){
    int hour, minute;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter the hour and minute: ");
    hour = sc.nextInt();
    minute = sc.nextInt();
    if(hour > 12 || hour < 1){
        System.out.println("Error: hour must be between 1 and 12");
        System.exit(0);
    }
    if(minute > 59 || minute < 0){
        System.out.println("Error: minute must be between 0 and 59");
        System.exit(0);
    }
    System.out.println("The hands are " + findAngle(hour, minute) + " degrees apart");
    }
    
    public static int findAngle(int hr, int min){
    int hrdeg = (DEGREES / HOURS) * hr;
    int mindeg = (DEGREES / MINUTES) * min;
    int angle;
    
    if(hr == 12){
        hrdeg = 0;
    }
    if(min == 0){
        mindeg = 0;
    }
    
    if(hrdeg < mindeg){
        angle = hrdeg + mindeg;
        if(angle > DEGREES / 2){
        angle = DEGREES - angle;
        }
    }
    else{
        angle = hrdeg - mindeg;
        if(angle > DEGREES / 2){
        angle = DEGREES - (hrdeg - mindeg);
        }
    }
    return angle;
    }
    

    } '''

    reply permalink

  • jedrek - 10 years, 3 months ago

    Am I out of it, but shouldn't the example be 180?

    reply permalink

  • Johnathan - 10 years, 3 months ago

    No, because the hour hand is only on the 12 at exactly 12:00. at 12:30 the hour hand is halfway between the 12 and the 1.

    reply permalink

  • Anonymous - 10 years, 3 months ago

    /**
     * ClockAngle.java
     * Calculates the angle between the hands of an
     * analogue clock given the value of the hour and
     * minute hands
     */
    
    import java.util.Scanner;
    
    public class ClockAngle{
    
        private static final int DEGREES = 360;
        private static final int HOURS = 12;
        private static final int MINUTES = 60;
    
        public static void main(String [] args){
        int hour, minute;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the hour and minute: ");
        hour = sc.nextInt();
        minute = sc.nextInt();
        if(hour > 12 || hour < 1){
            System.out.println("Error: hour must be between 1 and 12");
            System.exit(0);
        }
        if(minute > 59 || minute < 0){
            System.out.println("Error: minute must be between 0 and 59");
            System.exit(0);
        }
        System.out.println("The hands are " + findAngle(hour, minute) + " degrees apart");
        }
    
        public static int findAngle(int hr, int min){
        int hrdeg = (DEGREES / HOURS) * hr;
        int mindeg = (DEGREES / MINUTES) * min;
        int hrdeg2 = (min / 2);
        int angle;
    
    
        if(hr == 12){
            hrdeg = 0;
        }
        if(min == 0){
            mindeg = 0;
        }
    
        if(hrdeg < mindeg){
            angle = hrdeg + mindeg + hrdeg2;
            if(angle > DEGREES / 2){
            angle = DEGREES - angle;
            }
        }
        else{
            angle = hrdeg - mindeg - hrdeg2;
            if(angle > DEGREES / 2){
            angle = DEGREES - (hrdeg - mindeg);
            }
        }
        return angle;
        }
    }
    

    reply permalink

Content curated by @MaxBurstein