-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContandoNumerosPares.java
More file actions
32 lines (29 loc) · 976 Bytes
/
ContandoNumerosPares.java
File metadata and controls
32 lines (29 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Desafio 3
Dado um inteiro positivo num, retorne o número de inteiros positivos menor ou igual a num cuja
soma de dígitos é par. A soma dos dígitos de um inteiro positivo é a soma de todos os seus dígitos.
*/
import java.util.*;
public class ContandoNumerosPares{ public static void main(String[] args){
int num = Integer.parseInt(new Scanner(System.in).nextLine());
var count = 0;
for (var i = 1; i <= num; i++) {
var strNum = String.valueOf(i);
if (strNum.length() == 1) {
if (i % 2 == 0) {
count++;
}
continue;
}
char[] vs = strNum.toCharArray();
var sum = 0;
for (var j = 0; j < vs.length; j++) {
sum += (int)Character.getNumericValue(vs[j]);
}
if (sum % 2 == 0) {
count++;
}
}
System.out.println(count);
}
}