Problem:
Given three ints, a b c, return true if two or more of them have the same rightmost digit. The ints are non-negative. Note: the % "mod" operator computes the remainder, e.g. 17 % 10 is 7.
lastDigit(23, 19, 13) → true
lastDigit(23, 19, 12) → false
lastDigit(23, 19, 3) → true
Solution:
public boolean lastDigit(int a, int b, int c) { int modA = a % 10; int modB = b % 10; int modC = c % 10; if (modA == modB || modA == modC || modB == modC) return true; else return false; }
public boolean lastDigit(int a, int b, int c) {
ReplyDeleteint modA = a % 10;
int modB = b % 10;
int modC = c % 10;
return (modA == modB || modA == modC || modB == modC);
}
Or you could do an one linear
return a % 10 == b % 10 || a % 10 == c % 10 || c % 10 == b % 10;
ReplyDeleteint aR = a % 10;
ReplyDeleteint bR = b % 10;
int cR = c % 10;
return !(aR != bR && bR != cR && aR != cR);
public boolean lastDigit(int a, int b, int c) {
ReplyDeleteint aRem = a%10;
int bRem = b%10;
int cRem = c%10;
if(aRem != bRem && bRem != cRem && aRem != cRem){
return false;
}
return true;
}
public boolean lastDigit(int a, int b, int c) {
ReplyDeletereturn (a % 10 == b % 10) != (b % 10 == c % 10) != (c % 10 == a % 10);
}
if((a % 10 == b % 10)||(a % 10 == c % 10) || (b % 10 == c % 10)) return true;
ReplyDeleteelse return false;
return((a%10==b%10||a%10==c%10||b%10==c%10));
ReplyDelete