+ 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
4 Respostas
+ 2
This should work too:
sum += ((x%10)%2==0? x%10: 0);
+ 1
Sum of even digits or digits on even positions in number?
What should be the output of your sample input number(244)?
+ 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();
0
Thanks you so much