Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

even digit sum java

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;
        }
    }
}
 
PREVIOUS NEXT
Tagged: #digit #sum #java
ADD COMMENT
Topic
Name
6+7 =