Joshua_D
Joshua_D

Reputation: 11

Find if int x is within 10 of closest hundred

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

Answers (1)

MikeCAT
MikeCAT

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

Related Questions