-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstLastDigitSum.java
More file actions
37 lines (29 loc) · 826 Bytes
/
Copy pathFirstLastDigitSum.java
File metadata and controls
37 lines (29 loc) · 826 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
public class FirstLastDigitSum {
public static void main(String[] args) {
System.out.println(sumFirstAndLastDigit(5));
}
public static int sumFirstAndLastDigit(int number) {
if (number < 0) {
return -1;
}
int sum = 0;
int firstDigit = number % 10;
int lastDigit = 0;
// System.out.println(firstDigit);
while (true) {
if(number < 10){
lastDigit = number;
sum = firstDigit + lastDigit;
break;
}
if (number < 99) {
lastDigit = number / 10;
System.out.println(lastDigit);
sum = firstDigit + lastDigit;
break;
}
number /= 10;
}
return sum;
}
}