how to get the same repeating numbers for any of my imput number?

Started by
1 comment, last by KillerkoUK 9 years, 4 months ago

I have input numbers:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.... etc

I would like to get output for each of these nunbers to be:

1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2... etc

so for my input 12 i wanna get 4 and for 13 it would be 1 and so on

is there any simple way how I could get that?

for example 13 - ( int(13/4) * 4 ) is 1

that works for 14 or 15 too, but unfortunately not for 12 or 16 as i will get 0 which is wrong

Advertisement

You want the modulo operator, which essentially gives you the remainder of a division (although there are some subtle differences for negative numbers)

It might look like 'mod' or '%' depending on the language.

For your specific case, try: ((n - 1) % 4) + 1

Yes, Thank you C0lumbo!

Thats exactly what I needed..

I will never have a negative number so this is more than enough for me.

This topic is closed to new replies.

Advertisement