# Formula for difference between two numbers on a wall clock in clokwise direction

It's my first post in math.stackexchange.com. I got a necessity to find out the clockwise difference between two numbers on a wall clock. For example, difference between 12 and 1 is 1 where as the difference between 1 and 12 is 11. Actually what I need is number of clock wise steps required reach number b from number a on a clock. While writing an application, I struck writing this function.

• You could use arithmetic modulo 12.
– A.P.
Commented May 11, 2013 at 11:45
• can someone tell me why down vote for my question please.... Commented May 11, 2013 at 13:27
• Probably you got caught in the debate on PSQs... or simply because this is a duplicate question.
– A.P.
Commented May 11, 2013 at 13:39

i am writing a C function

Code

 int clockstep(int a, int b){
int steps =0;
if (a<=b)
{
steps = b-a;
}
else
{
steps = 12-b+a;
}
return steps
}


means you take input from user than compare it if 1st $<=$ 2nd then simply (2nd-1st) is your answer otherwise do (12-2nd+1st).

• i have written logic and answer below the code Commented May 11, 2013 at 12:26
• I think your else clause should be $12+b-a$ since the two answers should agree mod $12$.
– robjohn
Commented May 11, 2013 at 12:43
• else is (12-b+a) what is wrong. Commented May 11, 2013 at 12:47
• $12-b+a\not\equiv b-a\pmod{12}$ Try some numbers and see if you get what you want. For example, $12$ and $1$, then $1$ and $12$.
– robjohn
Commented May 11, 2013 at 12:51
• Thanks everybody and thanks @iostream007.Is it not 12-a+b? I think it is! I'm new to this site, can anybody tell me why negative vote for my question? Commented May 11, 2013 at 13:13