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

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