+ 2

Java help

public class Program { public static void main(String[] args) { System.out.println(l(244)); } static int l(int x ){ int sum=0; while(x!=0){ sum=sum+(x%10); x=x/10; } return sum; } } I have code like this, but I don't know how to find sum only Even digits of some number

24th Feb 2019, 7:10 PM
Kasia Herasymenko
Kasia Herasymenko - avatar
4 Respostas
+ 2
This should work too: sum += ((x%10)%2==0? x%10: 0);
24th Feb 2019, 10:52 PM
Adrien Zettl
Adrien Zettl - avatar
+ 1
Sum of even digits or digits on even positions in number? What should be the output of your sample input number(244)?
25th Feb 2019, 3:06 AM
vlada
+ 1
If I understood you, better use streams for such things and filter it how you want. Just an ex.: int sum = IntStream.range(1, 245). filter(x -> x % 2 == 0). sum();
5th Mar 2019, 11:27 AM
M_N
0
Thanks you so much
24th Feb 2019, 8:49 PM
Kasia Herasymenko
Kasia Herasymenko - avatar