public class Main {
public static void main(String[] args) {
System.out.println(getEvenDigitSum(1221));
// output is 4
}
public static int getEvenDigitSum (int number) {
if (number >= 0) {
int sum = 0;
int lastDigit = 0;
while (number > 0) {
if ((number % 2) == 0) {
lastDigit = number % 10;
sum += lastDigit;
}
number /= 10;
}
return sum;
} else {
return -1;
}
}
}