# Find Out The Angle Between Hour Hand And Minute Hand In C#

Introduction

In this article, we will learn how to calculate the angle between the two hands of the clock in C#.

Step 1

Let try to understand the clock's behavior.

The clock has two different measurements problems,

• Angles (calculated in degree from  number 12 clockwise)
• Times (calculated usually based on a 12-hour clock.)

Let’s take an example: The time is 1:20 PM

From the above screen shot let’s calculate the angle between the hour hand and minute hand.

We can divide the complete clock into  12 gaps and each gap with 300.

• Angle of Seg0  =| (Angle of Seg1) – (Angle of Seg2) |
• Since Seg1 has 4 gaps then total angle will be =4*300=1200
• Since Seg2 has 1 gap and one sub gap g1 then total angle will be =1*300 +g1

To get angle of seg0 we have to find out sub gap g1 angle so let's try to use the above formula to calculate the value of sub gap g1.

Since we know that the angle covered by hour hand in one minute =0.50 and the hour hand covered g1 segment in 20 minutes so we can say that angle covered by sub gap g1 will be =20*0.50 =100.

so Seg2 has 1 gap and one sub gap g1 then total angle will be =1*300 +100=400

Now Angle of Seg0 =|120-40|=800.

Similarly, we can calculate the angle of a given point of time.

Let’s try to build a formula to calculate angle: if you see from above example then you can build the formula.

Time is H:M

Let's consider the rate of change of the angle in degrees per minute.

• Angle covered by hour hand in 12 hours = 3600
• 01 hour = 300
• 01 min= (30/60) ==0.50 = (1/2)0
• Angle covered by minute Hand in 05 minutes =300
• 01 minutes =60

Angle covered by hour hand qh= (H*60*0.5+M*0.5) = 0.5(h*60+M)0

Angle covered by minute hand qm=(M*6)0

Total angle between hour & minute hand will be ∆q= | qh - qm |

∆θ = | (0.5(h*60+M)0) - (M*6)0 |=|30H-5.5M=5(6H-1.1M) |0=|5(6H-11/10M) |0

∆θ=|5(6H-1.1M) |0 Clockwise

Where H is hour and M is minute we are ignoring seconds here.

Let’s try to validate this formula. The time is 1:20 PM

As per formula angle between the hour and minute hand will be = |5(6*1-1.1*20) |0

=|5(6-22) |0=|5*(-16) |0=800 this is the same angle we have calculated previously in an example.

Now let’s try to write a method to calculate the angle between the hour and minute hand.

Step 2

Create a test project and create a cs file with name “Smartclock.cs” and create a function “GetAngleBetweenHourAndMinuteHand” and add the below lines of code into it.

1. using System;
2.
3. namespace CalculateAngleHMhands
4. {
5.     class SmartClock
6.     {
7.         public double GetAngleBetweenHourAndMinuteHand(int hour,int minute)
8.         {
9.             //∆θ=|5(6H-11/10M) |0
10.             //hour =>H
11.             //minute=>M
12.             return Math.Abs(5*((6 * hour)-(1.1*minute)));
13.         }
14.     }
15. }

Step 3

In the test class, write a few tests to validate this formula.

1. using Microsoft.VisualStudio.TestTools.UnitTesting;
2.
3. namespace CalculateAngleHMhands
4. {
5.     [TestClass]
6.     public class UnitTest1
7.     {
9.         public UnitTest1()
10.         {
11.             smartClock = new SmartClock();
12.         }
13.
14.         [TestMethod]
15.         public void TestMethod_0_00_PM()
16.         {
17.             double angle = smartClock.GetAngleBetweenHourAndMinuteHand(0, 00);
18.             Assert.AreEqual(0, angle);
19.         }
20.
21.         [TestMethod]
22.         public void TestMethod_12_30_PM()
23.         {
24.             double angle = smartClock.GetAngleBetweenHourAndMinuteHand(12, 30);
25.             Assert.AreEqual(195, angle);
26.         }
27.
28.         [TestMethod]
29.         public void TestMethod_1_20_PM()
30.         {
31.             double angle = smartClock.GetAngleBetweenHourAndMinuteHand(1, 20);
32.             Assert.AreEqual(80, angle);
33.
34.         }
35.         [TestMethod]
36.         public void TestMethod_2_20_PM()
37.         {
38.             double angle = smartClock.GetAngleBetweenHourAndMinuteHand(2, 20);
39.             Assert.AreEqual(50, angle);
40.         }
41.
42.     }
43. }

Now, you can see you developed a formula and it is working perfectly fine.

Congratulations you have successfully created and calculated the angle between the clock's hour and minute hand. If you have any queries or concerns just let me know or put them in the comment box and I will respond as soon as possible. I am open to discussing anything, even silly questions as well. If you have any suggestions related to this article, please let me know and I promise I will improve this article.

That's all for this tutorial.

Summary

In this article, we have learned how to calculate the angle between a clock's hour and minute hand in C#.