Java > Warmup-1 > nearHundred (CodingBat Solution)

Problem:

Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number.

nearHundred(93) → true
nearHundred(90) → true
nearHundred(89) → false


Solution:

public boolean nearHundred(int n) {
  int sum1 = Math.abs(n - 100);
  int sum2 = Math.abs(n - 200);
  
  if (sum1 <= 10 || sum2 <= 10)
    return true;
  else
    return false;
}


3 comments :

  1. public boolean nearHundred(int n) {
    return (90 <= n && n <= 110) || (190 <= n && n <= 210);
    }

    ReplyDelete
  2. return n>=90&&n<=110||n>=190&&n<=210;

    ReplyDelete
  3. for both sum1 and sum2 the value should be less than or equal to 10 so the code will be as
    if(sum1<=10 || sum2<=10)

    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