Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a number is either a multiple of ten or within a particular set of ranges

I have a few loops that I need in my program. I can write out the pseudo code, but I'm not entirely sure how to write them logically.

I need -

if (num is a multiple of 10) { do this }  if (num is within 11-20, 31-40, 51-60, 71-80, 91-100) { do this } else { do this } //this part is for 1-10, 21-30, 41-50, 61-70, 81-90 

This is for a snakes and ladders board game, if it makes any more sense for my question.

I imagine the first if statement I'll need to use modulus. Would if (num == 100%10) be correct?

The second one I have no idea. I can write it out like if (num > 10 && num is < 21 || etc.), but there has to be something smarter than that.

like image 414
user3419168 Avatar asked Apr 27 '14 23:04

user3419168


People also ask

How do you tell if a number is a multiple of 10?

When you multiply a number by 10 you will get the number attached with zero as the result. eg: 10 × 2 = 20. Multiples of 10 are even numbers that end with 0. If a number is ending with 0 then it is always divisible by 10.

How do you check if a number is within a range?

If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0. And must be smaller than or equal to high i.e., (high – x) <= 0. So if result of the multiplication is less than or equal to 0, then x is in range.

How do you find the multiples of a number in a range?

Thus including 20 and 300 it has 60−4+1=57 multiples. If suppose instead of 300, you had x on the end where x is not a multiple of 5 then you just take first multiple of 5 when you start walking left on integer line from x, i.e. if x=304, you replace it by 300 again.

How do you determine if a number is a multiple of another number?

When one number can be divided by another number with no remainder, we say the first number is divisible by the other number. For example, 20 is divisible by 4 ( ). If a number is divisible by another number, it is also a multiple of that number. For example, 20 is divisible by 4, so 20 is a multiple of 4.


2 Answers

For the first one, to check if a number is a multiple of use:

if (num % 10 == 0) // It's divisible by 10 

For the second one:

if(((num - 1) / 10) % 2 == 1 && num <= 100) 

But that's rather dense, and you might be better off just listing the options explicitly.


Now that you've given a better idea of what you are doing, I'd write the second one as:
   int getRow(int num) {       return (num - 1) / 10;    }     if (getRow(num) % 2 == 0) {    } 

It's the same logic, but by using the function we get a clearer idea of what it means.

like image 94
Winston Ewert Avatar answered Oct 08 '22 11:10

Winston Ewert


if (num is a multiple of 10) { do this }

if (num % 10 == 0) {   // Do something } 

if (num is within 11-20, 31-40, 51-60, 71-80, 91-100) { do this }

The trick here is to look for some sort of commonality among the ranges. Of course, you can always use the "brute force" method:

if ((num > 10 && num <= 20) ||     (num > 30 && num <= 40) ||     (num > 50 && num <= 60) ||     (num > 70 && num <= 80) ||     (num > 90 && num <= 100)) {   // Do something } 

But you might notice that, if you subtract 1 from num, you'll have the ranges:

10-19, 30-39, 50-59, 70-79, 90-99 

In other words, all two-digit numbers whose first digit is odd. Next, you need to come up with a formula that expresses this. You can get the first digit by dividing by 10, and you can test that it's odd by checking for a remainder of 1 when you divide by 2. Putting that all together:

if ((num > 0) && (num <= 100) && (((num - 1) / 10) % 2 == 1)) {   // Do something } 

Given the trade-off between longer but maintainable code and shorter "clever" code, I'd pick longer and clearer every time. At the very least, if you try to be clever, please, please include a comment that explains exactly what you're trying to accomplish.

It helps to assume the next developer to work on the code is armed and knows where you live. :-)

like image 28
Adam Liss Avatar answered Oct 08 '22 12:10

Adam Liss