Reputation: 11
Total noob question, but I am in my first Java class and working though a problem set. I know how to see if a number is in a specific range (posted below) but I am trying to find if this holds true of any of the nearest hundreds.
The rules are N has to be within 10 of either side of nearest 100.
if (n >= 90 && n <= 110) {
return true;
} else {
return false;
}
Upvotes: 1
Views: 114
Reputation: 75062
You can use %
operator to calculate remainder of division.
int r = Math.abs(n) % 100; // use abs(), or r will be negative if n is negative
return r <= 10 || 90 <= r;
Upvotes: 2