views:

174

answers:

3

How do I return a number between 0 and 4, depending the input number?

For example if I pass it number 23 it will return 3. The number set should look like

0 5 10 15 20 ..

1 6 11 16 21 ..

2 7 12 17 22 ..

3 8 13 18 23 ..

4 9 14 19 24

What's the math for this?

+13  A: 

Use the modulo operation, typically % in many languages (and mod in many others).

10 % 5 = 0

17 % 3 = 2
Amber
+1. Ya beat me to it. Also called `return the remainder` in non-math speak. :-)
Atømix
+2  A: 

http://en.wikipedia.org/wiki/Modulo_operator

N % 5
Kevin Chan
+2  A: 

The above two answers are correct, the Modulo operator is very useful in calculating remainders. For example the "pass 23 return 3" : 23 % 5 works as follows: 5 goes into 23 4 times ( (int) 23 / 5 = 4) the modulo operater then gives you the remainder (23 - (5 * 4)), which happens to be the number you and your assignment are looking for, 3.

Kevin Zhou