How to find the first and last digit of a number in Java

In this article, we are going to understand and perform a simple Java program, in which, we are going to find the first and last digits of a given number. For example, the number we have is 1234, so the first and last digits that we need to have here are 1 and 4 respectively. Here are some sample inputs and outputs for our program example –

Sample input and output –

Example 1 –

input –
Please enter the number: 1234
output –

first digit: 1
last digit: 4

Example 2 –

input –
Please enter the number: 9876
output –
first digit: 9
last digit: 6

Program Approach

Now, let’s discuss how are we going to achieve this task. The task is simple. We just need to get the first and last digits of the given number.

Getting the last digit of the given number

If you are given a number, and you need to get the last digit from the number, then it is a very simple task. In order to get the last digit of the given number, we just need to get the remainder when the number is divided by 10. So, this is the simple instruction to get the last digit –

int lastdigit = number%10;

Here, the number is the variable that has the number, from which we want to get the last digit. So, this is how we get the last digit. Getting the first digit is also simple, but it might seem a little bit tricky, so let’s see how we can get the first digit from the number.

Getting the first digit from the number

In order to get the first digit from the number, we can do some different things. Let’s try them one by one here.

Way 1 –

To get the first digit from the number, we just keep dividing the number by 10, till the number becomes less than 10. Once the number is less than 10, we are only left with 1 digit, which is our first digit.

Way 2 –

To get the first digit from the number, we just need to divide the number by the (n-1)th power of 10, where n is the number of digits in the given number.

For example, if we have the number 1234, and we want to get the first number, we just need to divide the number 1234 by 1000. So here, 1000 comes out to be 10 to the power 3, and here 3 is one less than the number of digits.

So, this is how we get the first and the last digit from the given number. You can try to get the first digit using the way 1, but in the below program that we are going to discuss, we are going to get the first digit using the way 2. (the first way is also given, within the comment)

How to find the first and last digit of a number in Java

import java.util.Scanner;
public class Main
{

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Please enter the number:”);
int number = scanner.nextInt();
int lastDigit = number%10;
// You can print the number variable, to get the last digit.
/* while(number>=10)
{
number /=10;
}
*/
int firstDigit = number/((int)Math.pow(10, (int)Math.log10(number)));
System.out.println( “The first digit:” + firstDigit);
System.out.println(“The last digit:” +lastDigit);
}
}

As you can see in the above program, we have taken the number as user input, and then we are simply getting the last digit by getting the remainder of the number when we divide the number by 10.

To get the first digit, we have demonstrated both ways, and you can use the one you are comfortable understanding. Let’s have a look at the output of the above program.

Example output 1 –

Please enter the number:12345
The first digit:1
The last digit:5

Example output 2 –

Please enter the number:4857
The first digit:4
The last digit:7

As you can see, we are able to get the first and last digits from the given number. You can also try the above program for different inputs.

Conclusion –

In this article, we have understood and performed a simple Java program to get the first and last digits from the given number. Getting the last digit is very easy, and we have seen some ways to get the first digit from the number. You can also try the above program for more inputs, and explore more such related programs. Good luck!