Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Clock Angle Calculation

Find the smaller angle between clock hands with Python. Students account for the moving hour hand and compare the two arcs on a twelve-hour clock face.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 4 of 10
clock-angle-calculation.py
1# Take time
2hours = int(input("Enter hours (1-12): "))
3minutes = int(input("Enter minutes (0-59): "))
4
5# Calculate angles
6hour_angle = (hours % 12) * 30 + minutes * 0.5
7minute_angle = minutes * 6
8
9# Find smaller angle
10angle = abs(hour_angle - minute_angle)
11smaller_angle = min(angle, 360 - angle)
12
13print(f"Smaller angle: {smaller_angle} degrees")

Output

Enter hours (1-12): 3
Enter minutes (0-59): 30
Smaller angle: 75.0 degrees

Enter hours (1-12): 12
Enter minutes (0-59): 0
Smaller angle: 0.0 degrees

In this one

  • Model moving clock hands
  • Compute an absolute angular gap
  • Choose the smaller arc

How it works

  1. 1

    Calculate hour and minute angles.

  2. 2

    Take the absolute difference between them.

  3. 3

    Choose min(angle, 360 - angle) and print.

Edge cases

Why use hours % 12?

Why use hours % 12?

It maps twelve to the same starting position as zero.

What happens at 12:00?

What happens at 12:00?

Both hand angles are zero, so the smaller angle is zero.

What's going on

The hour hand moves half a degree each minute, not just thirty degrees at the hour. The formula combines the hour position with that minute-by-minute movement.

The minute hand moves six degrees per minute. abs finds the direct gap, and min compares that gap with the complementary arc around the full circle. At 3:30 the hour hand is at 105 degrees and the minute hand at 180, giving 75 degrees.

Why use hours % 12? It maps twelve to the same starting position as zero.

What happens at 12:00? Both hand angles are zero, so the smaller angle is zero.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why not use hours * 30 alone?

Pick an answer to check it.

Question 2

Why use hours % 12?

Pick an answer to check it.

Questions people ask

Why use hours % 12?

It maps twelve to the same starting position as zero.

What happens at 12:00?

Both hand angles are zero, so the smaller angle is zero.

Why not use hours * 30 alone?

The hour hand also moves during the hour.