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;
}
}
}
Code Example |
---|
:: |
Java :: |
:: |
Java :: |
:: |
:: print to console in java |
Java :: |
Java :: java run shell command |
:: java get random char |
:: how to add minutes in date in java |
Java :: |
Java :: |
:: java scanner input float |
:: |
Java :: sum numbers in array java |
Java :: What is a point class in java? |
Java :: list set value at index java |
Java :: java string remove more than one space |
Java :: |
Java :: java get variable from another class |
Java :: java uuid |
Java :: boolean print condition java |
:: |
Java :: java argument main |
:: javafx stackpane set position |
Java :: float maximum value java |
:: |
Java :: JAVA HashMap get keys by values |
:: |
Java :: remove all new line and other in string using java |