Java > Logic-1 > nearTen (CodingBat Solution)

Problem:

Given a non-negative number "num", return true if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2. See also: Introduction to Mod

nearTen(12) → true
nearTen(17) → false
nearTen(19) → true


Solution:

public boolean nearTen(int num) {
  if (num % 10 < 3 || num % 10 >=8)
    return true;
  else return false;
}


9 comments :

  1. Replies
    1. What is the significance of 5 in this code?

      Delete

  2. Your cell phone rings. Return true if you should answer it. Normally you answer, except in the morning you only answer if it is your mom calling. In all cases, if you are asleep, you do not answer.

    ReplyDelete
  3. public boolean nearTen(int num) {
    return num % 10 <= 2 || 10 - num % 10 <= 2;
    }

    ReplyDelete
  4. public boolean nearTen(int num) {
    return (num % 10 <= 2) != (num % 10 >= 8);
    }

    ReplyDelete
  5. public boolean nearTen(int num) {
    return num%10 <= 2 || num%10 >= 8;
    }

    ReplyDelete
  6. public boolean nearTen(int num) {
    return num%10==0 || num%10==1 || num%10==2 || num%10==8 || num%10==9;
    }

    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