Write a method called sumFirstAndLastDigist() with one parameter of type int called number.
The methods needs to find the first and Last digist of the parameter number passed to the method, using a loop and return the sum of the first and Last digist
public class Main {
static int sumFirstAndLastDigits(int number) {
int first = 0;
int last = 0;
boolean isLast = false;
while (Math.abs(number) > 0) {
if (!isLast) {
last = Math.abs(number % 10);
isLast = true;
}
if (Math.abs(number) < 10) {
first = Math.abs(number % 10);
}
number /= 10;
}
return first + last;
}
}
Comments
Leave a comment