The challenge for this month’s Exercise is to report the angle between two hands on an analog clock. The result must be positive and it must reflect the lesser of two possible degrees.
I plowed through a few different solutions before I was happy with the following:
2026_10-Exercise.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int hour,minute,h_angle,m_angle,degrees;
/* gather input */
printf("Enter the hour: ");
scanf("%d",&hour);
printf("Enter the minute: ");
scanf("%d",&minute);
/* confirm input */
if( hour<1 || hour>12 )
{
printf("Invalid hour: %d\n",hour);
exit(1);
}
if( minute<0 || minute>59 )
{
printf("Invalid minute: %d\n",minute);
exit(1);
}
/* calculate angle */
h_angle = hour * (360/12); /* 12 hours on the clock */
m_angle = minute * (360/60); /* 60 minutes on the clock */
if( hour > minute )
degrees = abs(m_angle-h_angle);
else
degrees = abs(h_angle-m_angle);
/* ensure that the smallest angle is set */
degrees = 360-degrees < degrees ? 360-degrees : degrees;
/* inform user */
printf("The angle for %d:%02d is %d degrees\n",
hour,minute,degrees);
return 0;
}
The first part of the code fetches standard input and tests the results. The values entered must be in range: 1 through 12 for the hour and 0 through 59 for the minute.
The second part of the code calculates the angle. The hour angle (h_angle) is the hour value times 360 degrees divided by the 12 hours on the clock:
h_angle = hour * (360/12);
The minute angle (m_angle) is the minute value times 360 degrees divided by the 60 minutes on a clock:
m_angle = minute * (360/60);
The answer is the difference between the two calculated angles, h_angle and m_angle, but the result must be positive. So, for the third part of my solution a comparison is made:
if( hour > minute )
The result determines whether the angle for hours is subtracted from minutes or vice-versa. In both cases, I use the abs() function to ensure that the result is positive. (The abs() function requires inclusion of the math.h header file, but in Linux you do not need to link in the math library for the function to behave.)
Finally, to ensure that the smallest angle is always represented, a ternary statement sets the result into int variable degrees:
degrees = 360-degrees < degrees ? 360-degrees : degrees;
The result is output.
Here are a few sample runs:
Enter the hour: 12 Enter the minute: 0 The angle for 12:00 is 0 degrees Enter the hour: 4 Enter the minute: 55 The angle for 4:55 is 150 degrees Enter the hour: 6 Enter the minute: 29 The angle for 6:29 is 6 degrees
My approach involves what appears to be a lot of redundancies. Even so, I tested the options and used various inputs. So, what I came up with, even if it’s aggressive, works. I hope that your solution met with success. As I wrote earlier, many solutions are possible for this programming puzzle. I saw a clever one on line that makes me wish I thought of it. Some solutions can be quite clever and elegant.
I didn’t get round to coding anything but it occurred to me that to be absolutely accurate the hour angle needs to have 1/12 of the minute angle added to it, eg.
m_angle = minute * (360/60);
h_angle = hour * (360/12) + (m_angle/12);
If you point the hour hand of your watch at the Sun then south is half way between the two hands. A useful addition to this code would be to calculate the minute where south is.
It’s not entirely accurate because “official” time is the same across a whole time zone so can vary quite a bit from Solar Time. (Especially in China which has only one time zone despite being roughly as wide as the USA.) Also you need to allow for some countries putting time forward in the spring and back in the autumn. I’m not sure whether the US does that?
I suppose if it were a truly analog clock, more math is required to accurately calculate the hands’ positions. E.g., the hour hand doesn’t just magically jump from the 1 to the 2 while the minute hand is sweeping ’round. Okay. A problem for another day…
Yes, regrettably, the US still practices the insane behavior of “Daylight Saving Time.” Next month, the clocks fall back an hour. People hate it, but our government would rather bicker and throw tantrums than address the issue.