Find First and Last Digit of a Number in Java

In this post, I will be sharing a java program on how to find the first and last digit of a number. There are 3 ways to achieve our goal:
a. Using while loop
b. Using the pow() method and while loop
c. Using pow() and log() methods

First, we will understand the question by writing the examples:

Input Number: 123456
First Digit: 1, Last Digit: 6
Input Number: 8041989
First Digit: 8, Last Digit:9


Algorithm:

1. Take the input from the user using the Scanner class object.
2. Create variables firstDigit and lastDigit and initialize them to 0.
3. Use the modulo operator to find the lastDigit.
4. Using while loop, Math.log(), and Math.pow() methods we can calculate the first digit of the number. Note: Last digit is easy to calculate as we can use modulo operator(%).

1. Using while loop



The first digit can be calculated using while loop and division operator as shown in the example below:

import java.util.Scanner;
public class JavaHungry {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        System.out.println("Given number is: "+ number);
        int firstDigit = 0;
        int lastDigit = 0;
        /*Below is the way to calculate the 
        Last Digit of a Number*/
        lastDigit = number % 10;
        System.out.println("Last Digit is: " + lastDigit);        
        /*Below is the way to calculate the 
        First Digit of a Number*/
        while(number != 0)
        {
            firstDigit = number % 10;
            number = number / 10;
        }
        System.out.println("First Digit is: " + firstDigit);
    }
}

Output:

Given number is: 345678
Last Digit is: 8
First Digit is: 3

2. Using built-in methods: log() and pow() methods


First digit can also be calculated using built-in log() and pow() methods of Math class. a. log() method can be used to calculate the number of digits in the given number. b. pow() method can be used to get the divisor value that can be later used to calculate the first digit. You can find the java program to calculate the first and last digit of a number using log() and pow() method below.

import java.util.Scanner;
public class JavaHungry {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        System.out.println("Given number is: "+ number);
        int firstDigit = 0;
        int lastDigit = 0;
        /*Below is the way to calculate the 
        Last Digit of a Number*/
        lastDigit = number % 10;
        System.out.println("Last Digit is: " + lastDigit);        
        /*Below is the way to calculate the 
        First Digit of a Number*/
        int totalDigits = (int) Math.log10(number);
        firstDigit = (int) (number/ (int) Math.pow(10,totalDigits));
        System.out.println("First Digit is: " + firstDigit);
    }
}

Output:
Given number is: 1234567
Last Digit is: 7
First Digit is: 1

3. Using while loop and pow() method


The first digit can also be calculated using the while loop and pow() method of Math class. a. while loop can be used to calculate the number of digits in the given number using count variable. b. pow() method uses the count variable to get the divisor value that can be later used to calculate the first digit. You can find the java program to calculate the first and last digit of a number using the while loop and pow() method below.

import java.util.Scanner;
public class JavaHungry {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        System.out.println("Given number is: "+ number);
        int firstDigit = 0;
        int lastDigit = 0;
        int digitCount = 0;
        /*Below is the way to calculate the 
        Last Digit of a Number*/
        lastDigit = number % 10;
        System.out.println("Last Digit is: " + lastDigit);        
        /*Below is the way to calculate the 
        First Digit of a Number*/
        int num = number;
        while(num != 0)
        {
            digitCount++; 
            num = num / 10;
        }
        firstDigit = (int) number/ (int) Math.pow(10,digitCount-1);
        System.out.println("First Digit is: " + firstDigit);
    }
}

Output:
Given number is: 9876543
Last Digit is: 3
First Digit is: 9

That's all for today, please mention in comments in case you know any other way to find first and last digit of a number in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry