I'm given the following task, and can do it if I can use a string-
Write a method named
lastDigit
that returns the last digit of an integer. For example,lastDigit(3572)
should return2
. It should work for negative numbers as well. For example,lastDigit(-947)
should return7
.
Where this problem gets tricky for me is that I am not allowed to use a String
to solve this problem. Here's what I have thus far-
public static int lastDigit(int d) { // d is the integer they call
// i know that whatever goes here will be something like this
int b = charAt(length - 1);
return b;
}
Any tips? Thanks!