Java > Logic-1 > lessBy10 (CodingBat Solution)

Problem:

Given three ints, a b c, return true if one of them is 10 or more less than one of the others.

lessBy10(1, 7, 11) → true
lessBy10(1, 7, 10) → false
lessBy10(11, 1, 7) → true


Solution:

public boolean lessBy10(int a, int b, int c) {
  int high = Math.max(a,b);
  high = Math.max(high, c);
  
  if (high - a >= 10 || high - b >= 10 || high - c >=10)
    return true;
  else 
    return false;
}


15 comments :

  1. public boolean lessBy10(int a, int b, int c) {
    int max = Math.max(a,Math.max(b,c));
    int min = Math.min(a,Math.min(b,c));
    int dif = max - min;
    if (dif >= 10) return true;
    else return false;
    }

    ReplyDelete
  2. if (high - a >= 10 || high - b >= 10 || high - c >=10)
    return true;
    else
    return false;

    can be shortened to:

    return (high - a >= 10 || high - b >= 10 || high - c >=10)

    ReplyDelete
  3. return Math.abs(a - b) >= 10 || Math.abs(b - c) >= 10 || Math.abs(a - c) >= 10;

    ReplyDelete
  4. return Math.abs(a - b) >= 10 ? true
    : Math.abs(b - c) >= 10 ? true
    : Math.abs(a - c) >= 10 ? true
    : false;

    ReplyDelete
  5. return (Math.abs(a-b) >=10) || (Math.abs(b-c) >=10) || (Math.abs(c-a) >=10);

    ReplyDelete
  6. Can we do this program without using math.abs

    ReplyDelete
    Replies
    1. yes we can.


      public boolean lessBy10(int a, int b, int c) {
      if((a-b>=10) || (b-c>=10) || (c-a>=10) || (b-a>=10) || (c-b>=10) || (a-c>=10))
      {
      return true;
      }
      else
      {
      return false;
      }
      }

      Delete
  7. return Math.abs(a-b)>=10||Math.abs(a-c)>=10||Math.abs(c-b)>=10;

    ReplyDelete
  8. "Yes we can do program without using Math.abs"

    if(a-b>=10 || b-c>=10 || a-c>=10 || c-b>=10 || c-a>=10)
    {
    return true;
    }
    else
    {
    return false;
    }

    ReplyDelete
  9. return true ? (Math.abs(a-b) >= 10 || Math.abs(a-c) >= 10 || Math.abs(b-c) >= 10) : false;

    ReplyDelete
  10. public boolean lessBy10(int a, int b, int c) {
    return ((a-b>=10) || (b-a>=10) || (b-c>=10) || (c-b>=10) || (a-c>=10) || (c-a>=10));
    }

    ReplyDelete
  11. return ((a-b>=10) || (b-a>=10) || (b-c>=10) || (c-b>=10) || (a-c>=10) || (c-a>=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