-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddDigits.java
More file actions
27 lines (25 loc) · 752 Bytes
/
addDigits.java
File metadata and controls
27 lines (25 loc) · 752 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
package ag;
public class digits {
public static int addDigits(int num) {
while (Integer.toString(num).length() > 1) {
int len = Integer.toString(num).length();
int temp = 0;
for (int i = 0; i < len; i++) {
temp += Integer.parseInt(Integer.toString(num).split("")[i]);
}
num = temp;
}
return num;
}
public static void main(String[] args) {
// Given a non-negative integer num, repeatedly add all its digits until the
// result has only one digit.
// For example:
// Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only
// one digit, return it.
System.out.print(addDigits(0));
System.out.print(addDigits(38));
System.out.print(addDigits(192));
System.out.print(addDigits(92));
}
}