Java > Warmup-1 > max1020 (CodingBat Solution)

Problem:

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.

max1020(11, 19) → 19
max1020(19, 11) → 19
max1020(11, 9) → 11


Solution:

public int max1020(int a, int b) {
  int tempa;
  int tempb;
  
  if (a >= 10 && a <= 20)
    tempa = a;
  else
    tempa = 0;
  
  if (b >= 10 && b <= 20)
    tempb = b;
  else
    tempb = 0;
   
  return Math.max(tempa, tempb);
}


2 comments :

  1. if (a >= 10 && a <= 20 && b >= 10 && b <= 20){
    if (a > b) return a; return b;
    }
    else if (a >= 10 && a <= 20) return a;
    else if (b >= 10 && b <= 20) return b;
    return 0;

    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