Find angle between hour and minute hand (Clock Angle Problem)

Clock Angle Problem: Given time in HH:MM format, calculate the smallest angle between Hour hand and Minute hand.

Few examples shown in the table
Time1:002:307:0010:3011:203:405:158:45
Angle30°105°150°135°140°130°67½°7½°

In the above picture, small angle between hour and minute hand is 30° when the time is 1'o clock.

Explanation:

  • In 12 hours, hour hand rotates 360° , then in 1 hour it rotates 360°/12 = 30°
                                12 hours ---> 360° 
                                1 hour    ---> 360°/12 = 30°
  • In 60 minutes, minute hand rotates 360° , then in 1 minute it rotates 360°/60 = 6°
                                60 minutes ---> 360°
                                1 minute    ---> 360°/60 = 6°
  • Difference between these two angles is the angle between Hour hand & Minute hand.
  • If the angle is greater than 180° , then subtract it from 360° as we need small angle.

Java program to find angle between Hour and Minute hands :



Output :
Enter Time: 1:00
Small Angle b/w Hour and Min Hands : 30.0

Enter Time: 2:30
Small Angle b/w Hour and Min Hands : 105.0

Converting Decimal to Roman Numeral

In this post we will write a java program to convert a decimal number to equivalent Roman numeral.  Before we step in to the program, we will see what are the Roman symbols and its decimal values.


Roman Symbols and its values:
Roman SymbolDecimal value
I1
IV4
V5
IX9
X10
XL40
L50
XC90
C100
CD400
D500
CM900
M1000


Decimal Forming Rules:

1. When a symbol appears after a larger or equal symbol, then it is added
  • VI = V + I = 5 + 1 = 6
  • XXI = X + X + I = 10 + 10 + 1 = 21
2. But if the symbol appears before a larger symbol, the it is subtracted
  • IV = V - I = 5 - 1 = 4
  • IX = X - I = 10 - 1 = 9

Program :
With the below program, we can covert up to the maximum value 3000. If you want to covert more than 3000, just add more roman symbols in the thousands array.


Output:

Enter a decimal : 9
Roman is : IX

Enter a decimal : 30

Roman is : XXX

Enter a decimal : 2001

Roman is : MMI