Java > Logic-1 > less20 (CodingBat Solution)

Problem:

Return true if the given non-negative number is 1 or 2 less than a multiple of 20. So for example 38 and 39 return true, but 40 returns false. See also: Introduction to Mod

less20(18) → true
less20(19) → true
less20(20) → false


Solution:

public boolean less20(int n) {
    return (n + 1) % 20 == 0 || (n + 2) % 20 == 0;
}


3 comments :

  1. public boolean more20(int n) {
    return (n % 20 == 1) != (n % 20 == 2);
    }

    ReplyDelete
  2. public boolean less20(int n) {
    return (n%20 == 18 || n%20 == 19);
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact