Java Program to Find Second Last digit of a Number

In this tutorial, we will be solving the problem of Java program to find the second last digit of a number.

We will be going for short tricks for solving this kind of problem.

Java Program to Find Second Last Digit of a Number

In this tutorial, as discussed we will be solving the problem on Java program to find the second last digit of a number.

There can be applied various tricks while solving particular problem solving related questions.

In this question, while creating a class named check23 inside which we take an integer variable

Let us assign any value for the time being any small 3 digit value for checking.

The first task is to do is get the last digit, also the second last digit along with it.

For the above task, we will be taking the remainder of the above number and dividing it by 100.

After taking the modulus of the number, we will be getting the last two digits of the number.

Now since we have got the last two digits in our hand, we can move ahead for processing the numbers.

Since we have to take the only second last digit, we will be dividing the modulus remainder that we got above by 10.

Using this trick the last digit will be removed and we have the second last digit in our hand.

For displaying the value in an absolute manner, we will be using the method Math.abs.

Also read: How to count the number of digits in a given string in Java

Basically, this method displays the positive value of any value maybe int or any other datatype.

Then we will be displaying the result in the print method.

Find Second Last digit of a Number in Java Code:

public class check23
{
    	public static void main(String[] args)
    	{
    		int i = 123456;
    	 	System.out.print(Math.abs((i%100)/10));
    	}
}

After the successful compilation of the above code, we will be getting our desired outcomes.

Different values can be passed for getting different results for checking.

The sample output for the above program is as below:

Java Program to Find Second Last digit of a Number

Leave a Reply