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; }
public boolean love6(int a, int b) {
ReplyDeleteint sum = a + b;
int diff = Math.abs(a-b);
return (a == 6 || b ==6 || sum==6 || diff == 6) ? true:false;
}
public boolean love6(int a, int b) {
ReplyDeletereturn (a == 6 || b == 6 || Math.abs(a-b) == 6 || a+b == 6);
}
return ((a == 6 || b == 6 ) || (a+b == 6) || Math.abs(a-b) == 6);
ReplyDeletepublic static boolean love6(int a, int b) {
ReplyDeletereturn ((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?
public boolean love6(int a, int b) {
ReplyDeletereturn a == 6 || b == 6 || a + b == 6 || a - b == 6 || b - a == 6;
}
public boolean love6(int a, int b) {
ReplyDeleteint 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;
}
public boolean love6(int a, int b) {
ReplyDeleteint 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;
}
if(a==6 || a+b==6 || a-b==6 || b==6 || b-a==6)
ReplyDeletereturn true;
else return false;
public boolean love6(int a, int b) {
ReplyDeleteint 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;
}
public boolean love6(int a, int b) {
ReplyDeletereturn a==6 || b==6 ? true : a+b==6? true : Math.abs(a-b)==6 ? true : false;
}
public boolean love6(int a, int b) {
ReplyDeleteif(a==6 || b==6 || a+b==6 || Math.abs(a-b) == 6){
return true;
} return false;
}