Clock Angles

July 1, 2016

We have a fun little program for a lazy summer Friday:

Write a program that, given a time as hours and minutes (using a 12-hour clock), calculates the angle between the two hands. For instance, at 2:00 the angle is 60°.

Your task is to write a program that calculates clock angles. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

10 Responses to “Clock Angles”

  1. davor said

    Here’s my solution in APL.

    ANGLE←{|((⍵+⍺×60)×0.5)-⍵×6}

  2. Zack said

    Here is my take on the problem, using Julia:

    function convert_to_num{T <: AbstractString}(A::Array{T, 1})
    n = length(A)
    Z = zeros(n)

    for i = 1:n
    m = length(A[i])
    e = 0

    for j = m:-1:1
    Z[i] += (Float64(A[i][j]) – 48)*10^e
    e += 1
    end
    end

    return Z
    end

    function main(z::AbstractString)
    max_minutes = 720.0
    Z = convert_to_num(split(z, ":"))
    total_minutes = 60Z[1] + Z[2]
    angle_small_hand = 360total_minutes / max_minutes
    angle_large_hand = 6Z[2]
    temp = abs(angle_small_hand – angle_large_hand)
    return min(temp, 360.0 – temp)
    end

    BTW, the solution in the previous comment is off, since the angle of the hands is by default less or equal to 180 deg, while that solution allows for angles larger than 180 deg.

  3. Globules said

    A Haskell function.

    -- Given the time on a 12-hour clock return the angle in degrees
    -- between the hour and minute hands.  The angle can be measured from
    -- either hand to the other, whichever makes the angle the smallest.
    -- That is, both 12:01 and 12:59 are 6 degress rather than one of them
    -- being 354 degrees.
    clockAngle :: Int -> Int -> Int
    clockAngle h m = let deg = 6 * abs (5 * h - m)
                     in if deg < 180 then deg else 360 - deg
    
  4. fd said

    The solution page says:

    > (angle 3 45)
    157.5

    I’d have guessed 180.

  5. programmingpraxis said

    @fd: At 3:45, the minute hand is on the 9, but the hour hand has moved past the 3 and is 3/4 of the way to 4, making the angle less than 180°.

  6. Globules said

    Here’s an updated Haskell version, taking into account the movement of the hour hand as mentioned above.

    import Control.Monad (forM_)
    
    default (Int, Double)
    
    -- Given the time on a 12-hour clock return the angle in degrees between the
    -- hour and minute hands.  We ensure that the result is between 0 and 180
    -- degrees.
    -- 
    -- We account for the movement of the hour hand as the minutes increase.  For
    -- example, at 3:45 the hour hand will be 3/4 of the way towards 4, while at
    -- 12:59 it will be 59/60 of the way towards 1.
    --
    -- The angles are exact, to the minute.
    clockAngle :: Int -> Int -> Rational
    clockAngle h m = let (h', m') = (30 * fromIntegral h, 6 * fromIntegral m)
                         deg = abs (h' - 11/12 * m')
                     in if deg <= 180 then deg else 360 - deg
    
    main :: IO ()
    main = forM_ [(12, 00), (12, 01), (1, 20), (3, 45), (12, 59)]
      (print . fromRational . uncurry clockAngle)
    
    $ ./clockangle 
    0.0
    5.5
    80.0
    157.5
    35.5
    
  7. Sai Manish said

    import java.util.Scanner;

    public class clock_angles {

    public static void main(String[] args) {
    Scanner s = new Scanner(System.in); // TODO Auto-generated method stub
    double hh,mm;System.out.println(“enter hour”);hh=s.nextDouble();System.out.println(“enter min”);mm=s.nextDouble(); double h=hh+mm/60;
    double dh=360*h/12;
    double m=hh*60+mm;
    double mh=360*mm/60;
    System.out.println(Math.abs(dh-mh));
    }

    }

  8. Atif Farooq said

    #include
    #include
    #include
    #include
    using namespace std;
    //Function incorporating Solution.
    void Solution(void);
    int main(void)
    {
    Solution(); return 0;
    }
    class Angular_Time
    {
    public:
    //Function to Parse Time.
    void Set_Time(string);
    //Function to Compute Angle.
    int Get_Angle();
    //Constructor.
    Angular_Time(string);
    private:
    //Function to Compute Angle.
    void Compute_Angle();
    //Function to return int Equivalent.
    int toInt(char);
    //For Storing Time.
    int Hours, Minutes;
    //For Easier Parsing.
    string TimeStr;
    //Angle.
    int Angle;
    //for Computations.
    const int Hour_Jump, Minute_Jump;
    };
    void Solution(void)
    {
    //Gettting Time.
    string TakeTime;
    cin >> TakeTime;
    //Angle Object.
    Angular_Time MyAngle(TakeTime);
    //Result.
    cout << MyAngle.Get_Angle() << endl;
    }
    //**/Class Member Functions.//**/
    void Angular_Time::Set_Time(string Time)
    {
    //For Reference.
    (*this).TimeStr = Time;
    //Finding Hours and Minutes;
    if (TimeStr.length() == 4)
    {
    (*this).Hours =
    toInt(TimeStr[0]);
    (*this).Minutes =
    toInt(TimeStr[3]) + toInt(TimeStr[2]) * 10;
    }
    else
    {
    (*this).Hours =
    toInt(TimeStr[1]) + toInt(TimeStr[0]) * 10;
    (*this).Minutes =
    toInt(TimeStr[4]) + toInt(TimeStr[3]) * 10;
    }

    //Computing Angle.
    (*this).Compute_Angle();
    }
    void Angular_Time::Compute_Angle()
    {
    //Computing Angles for Hours.
    int Hour_Angle =
    (*this).Hours*Hour_Jump;
    //Computing Angles for Minutes.
    int Min_Angle =
    (*this).Minutes*Minute_Jump;
    int Differernce = abs(Hour_Angle – Min_Angle);
    //Edge Case.
    (Differernce == 360) ?
    ((*this).Angle = 0) : ((*this).Angle = Differernce);
    }
    int Angular_Time::toInt(char ConvChar)
    {
    return int(ConvChar) – 48;
    }
    int Angular_Time::Get_Angle()
    {
    return (*this).Angle;
    }
    Angular_Time::Angular_Time(string TimeStr)
    :Hour_Jump(30), Minute_Jump(6)
    {
    (*this).Set_Time(TimeStr);
    }

  9. Somewhat shorter in Forth in one someway

    The solution was reach by considering the problem as a whole (both equations for hours and minutes) and then simplified the resultant equation using math. This was done since the goal was only finding angles (not how to calculate them in stages – hours, minutes then the abs diff – but YMMV)
    from Wikipedia https://en.wikipedia.org/wiki/Clock_angle_problem
    hours = 0.5(60H + M)
    minutes = 6M

    Thus angle will be (without considering any hand as a reference)
    0.5(60H + M) – 6M
    30H + 0.5M – 6M
    30H – 5.5M
    30H – 11/2M
    since we are using absolute value after difference then we can reorder ’30H – 11/2M’ to ’11/2M – 30H’
    ’12 mod’ for when clock is 12

    The solution is at http://ideone.com/Ah603B

  10. sarang said

    class ClcokAngle
    {

    double angle;

    public:
    int min;
    int hour;
    ClcokAngle()
    {
    min=0;
    hour=0;
    angle=0;
    }
    ClcokAngle(int min,int hour)
    {
    this->min=min;
    this->hour=hour;
    this -> angle=0;
    }
    void Setmin(int min)
    {
    this->min=min;
    }
    void Sethour(int hour)
    {
    this->hour=hour;
    }
    int getmin()
    {
    return this->min;
    }
    int gethour()
    {
    return this->hour;
    }
    double GetAngle()
    {
    angle = 0.5*((60*hour)-(11*min));

    if (angle < 180)
    {
    return angle;
    }
    else
    {
    angle = 360-angle;
    return angle;
    }
    }

    };//end of class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: