Logic to Find First and Last Digit

Last Digit:

First Digit: