Clock Angle Calculation
Take time (hours and minutes) and print the smaller angle between the hour and minute hands.
AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 4 of 10
clock-angle-calculation.py
1# Take time2hours = int(input("Enter hours (1-12): "))3minutes = int(input("Enter minutes (0-59): "))45# Calculate angles6hour_angle = (hours % 12) * 30 + minutes * 0.57minute_angle = minutes * 689# Find smaller angle10angle = abs(hour_angle - minute_angle)11smaller_angle = min(angle, 360 - angle)1213print(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
What's going on
Calculate angles of hour and minute hands.
Key Concepts:
Hour hand: (hours % 12) * 30 + minutes * 0.5
Minute hand: minutes * 6
Find absolute difference
Take smaller of angle and 360 - angle
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum