Return second last digit of given number

In this topic, we will learn to get the second last digit of a number with the help of examples.
There can be several ways to get second last digit; here we are discussing some solutions.

Using modulo operator

Here, we are using modulo and divide operators to find the second last digit. The modulo operator gives remainder of a number while divide operator gives quotient as a result.

Output

2

Using String’s chartAt() method

There is another example in which we can convert the number into a string and then using charAt() method we get second last value after that result convert back to number using Character class.

Output

2

That’s all about how to return second last digit of given number.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *