-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP140.java
More file actions
31 lines (28 loc) · 832 Bytes
/
P140.java
File metadata and controls
31 lines (28 loc) · 832 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
package recursividad;
import java.util.Scanner;
//accepted
public class P140_2 {
static int sumarDigitos(int n) {
int resultado;
if (n < 10) {
return n;
} else {
resultado = n % 10 + sumarDigitos(n / 10);
}
return resultado;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String numero = sc.nextLine();
while (numero.charAt(0)!='-'){
int i;
for ( i = 0; i < numero.length()-1; i++) {
System.out.print(numero.charAt(i) + " + ");
}
System.out.print(numero.charAt(i) + " = ");
System.out.print(sumarDigitos(Integer.parseInt(numero)));
System.out.println();
numero = sc.nextLine();
}
}
}