Java > Logic-1 > old35 (CodingBat Solution)

Problem:

Return true if the given non-negative number is a multiple of 3 or 5, but not both. Use the % "mod" operator -- see Introduction to Mod

old35(3) → true
old35(10) → true
old35(15) → false


Solution:

public boolean old35(int n) {
    return n % 3 == 0 ^ n % 5 == 0;
}

7 comments:

  1. public boolean old35(int n) {
    return (n % 3 == 0) != (n % 5 == 0);
    }

    ReplyDelete
  2. public boolean old35(int n) {
    return (n%3==0&&n%5!=0)||(n%3!=0&&n%5==0);
    }

    ReplyDelete
  3. public boolean old35(int n) {
    if(n%3 ==0 && n%5 ==0){
    return false;
    }else if(n%3 ==0 || n%5 ==0){
    return true;
    }else{
    return false;
    }
    }

    ReplyDelete