-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitSum.java
More file actions
41 lines (30 loc) · 835 Bytes
/
Copy pathDigitSum.java
File metadata and controls
41 lines (30 loc) · 835 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
public class DigitSum {
int sum = 0;
public static void main(String[] args) {
sumDigits(25);
}
private static int sumDigits(int number) {
if (number >= 100) {
int x2 = number % 100;
int y2 = (number - x2) / 100; //y2 is hundreds
if (x2 >= 10) {
int x = x2 % 10;
int y = (x2 - x) / 10; // y is tens
int x3 = number - (y2 * 100) - (y * 10); // x3 is ones
int total = y2 + y + x3;
System.out.println(total + " total");
return y;
}
} else if ((number < 100) && (number >= 10)) {
int x = number % 10;
int y = (number - x) / 10;
int x3 = number - (y * 10);
int total = y + x3;
System.out.println(total + " total");
return y;
} else if (number < 10) {
return -1;
}
return number;
}
}