0

Help! I need to generate a user input (int) and get the last digit of the number. Please help.

I need to generate a user input: Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); That's not a problem ;) but I need to know get the last digit of the number. How can I do that? Please help me. Thank you so much. I can follow you if you lead me to the right way. ;)

28th Jun 2021, 1:54 PM
Yee Hoe
2 Respostas
+ 2
You can use: int lastDigit = number % 10; System.out.println(lastDigit);
28th Jun 2021, 2:12 PM
SammE
SammE - avatar
+ 1
do it like this, hope this help !! public static int lastDigit(int n) { return (n % 10); } System.out.println(lastDigit(n));
28th Jun 2021, 2:17 PM
JRAMAHES
JRAMAHES - avatar