1
$\begingroup$

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.

Please help me. Thanks in advance.

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

1 Answer 1

1
$\begingroup$

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).

$\endgroup$
6
  • $\begingroup$ i have written logic and answer below the code $\endgroup$ Commented May 11, 2013 at 12:26
  • $\begingroup$ I think your else clause should be $12+b-a$ since the two answers should agree mod $12$. $\endgroup$
    – robjohn
    Commented May 11, 2013 at 12:43
  • $\begingroup$ else is (12-b+a) what is wrong. $\endgroup$ Commented May 11, 2013 at 12:47
  • $\begingroup$ $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$. $\endgroup$
    – robjohn
    Commented May 11, 2013 at 12:51
  • $\begingroup$ 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? $\endgroup$ Commented May 11, 2013 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .