to get sum of even digits java

Solutions on MaxInterview for to get sum of even digits java by the best coders in the world

showing results for - "to get sum of even digits java"
Ada
14 Mar 2017
1//To find the sum of even digits in a given Number
2
3    public static int getEvenDigitSum(int number){
4        if(number<0){
5            return -1;
6        }
7        int finalNumber=0;
8        while(number>0){
9           if((number%10)%2==0){
10               finalNumber+=number%10;
11
12           }
13           number=number/10; //takes out last digit to test the next digit
14
15
16        }
17        return finalNumber;
18    }
19//Output: ex:number=12323 -- Ans: 4