-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejercicio2.java
More file actions
43 lines (29 loc) · 971 Bytes
/
ejercicio2.java
File metadata and controls
43 lines (29 loc) · 971 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
33
34
35
36
37
38
39
40
41
42
43
package introduccion_java_boletinIII;
public class ejercicio2 {
/*
* 2. Un número es divisible por 3 si la suma de todas sus cifras reducidas a una cifra es
igual a 0, 3, 6 ó 9.
Por ejemplo, 156 ⇒ 1+5+6=12 ⇒ 1+2 = 3 es divisible,
pero 157 ⇒ 1+5+7 =13 ⇒ 1+3 =4 no lo es.
Elabora un programa que compruebe la divisibilidad por 3 según este algoritmo. El
programa debe comprobar que el número facilitado es válido.
*/
public static void main(String[] args) {
System.out.println(divisible(156));
}
public static boolean divisible(int numero) {
int suma=0;
boolean mensaje=false;
String numeroCadena=String.valueOf(numero);
for(int i= 0; i<numeroCadena.length();i++) {
int numeroDigito= Character.getNumericValue(numeroCadena.charAt(i));
suma+= numeroDigito;
if (numeroDigito%3==0) {
mensaje=true;
}else {
mensaje=false;
}
}
return mensaje;
}
}