Java > Logic-1 > love6 (CodingBat Solution)

Problem:

The number 6 is a truly great number. Given two int values, a and b, return true if either one is 6. Or if their sum or difference is 6. Note: the function Math.abs(num) computes the absolute value of a number.

love6(6, 4) → true
love6(4, 5) → false
love6(1, 5) → true


Solution:

public boolean love6(int a, int b) {
  if (a == 6 || b == 6)
    return true;
    
  int sum = a+b;
  int diff = Math.abs(a-b);
  
  if (sum == 6 || diff == 6)
    return true;
  else
    return false;
}


11 comments :

  1. public boolean love6(int a, int b) {
    int sum = a + b;
    int diff = Math.abs(a-b);
    return (a == 6 || b ==6 || sum==6 || diff == 6) ? true:false;
    }

    ReplyDelete
  2. public boolean love6(int a, int b) {
    return (a == 6 || b == 6 || Math.abs(a-b) == 6 || a+b == 6);
    }

    ReplyDelete
  3. return ((a == 6 || b == 6 ) || (a+b == 6) || Math.abs(a-b) == 6);

    ReplyDelete
  4. public static boolean love6(int a, int b) {
    return ((a + b) == 6 || Math.abs(a - b) == 6);
    return (a == 6 || b == 6);
    }

    // Or

    public static boolean love6(int a, int b) {
    return (a + b) == 6;
    return (a == 6 || b == 6);
    return Math.abs(a - b) == 6;
    }

    these two blocks of code are not allowed. why?

    ReplyDelete
  5. public boolean love6(int a, int b) {
    return a == 6 || b == 6 || a + b == 6 || a - b == 6 || b - a == 6;
    }

    ReplyDelete
  6. public boolean love6(int a, int b) {

    int dif_ab = Math.abs(a - b);
    int dif_ba = Math.abs(b - a);

    // Check Numbers
    if ( a == 6 || b == 6 ){
    return true;
    }

    // Check Sum
    else if ( a + b == 6){
    return true;
    }

    // Check difference
    else if(dif_ab == 6 || dif_ba == 6){
    return true;
    }

    return false;
    }

    ReplyDelete
  7. public boolean love6(int a, int b) {

    int sum = a+b;
    int diff1 = a-b;
    int diff2 = b-a;

    if(a == 6 || b == 6)
    return true;
    else if (sum == 6)
    return true;
    else if(diff1 == 6 || diff2 == 6)
    return true;
    else
    return false;

    }

    ReplyDelete
  8. if(a==6 || a+b==6 || a-b==6 || b==6 || b-a==6)
    return true;
    else return false;

    ReplyDelete
  9. public boolean love6(int a, int b) {

    int diff = a> b ? a-b : b-a;

    if(a == 6 || b == 6) {
    return true;
    }else if(a+b == 6 || diff == 6) {
    return true;
    }

    return false;
    }

    ReplyDelete
  10. public boolean love6(int a, int b) {
    return a==6 || b==6 ? true : a+b==6? true : Math.abs(a-b)==6 ? true : false;
    }

    ReplyDelete
  11. public boolean love6(int a, int b) {
    if(a==6 || b==6 || a+b==6 || Math.abs(a-b) == 6){
    return true;
    } 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