Java – How to find/remove first and last digit of a number ?

In this article, we will learn how to find and remove first & last digit of a number

Find/remove first and last digit of a number :

  • Find the first digit of a number
  • Find the last digit of a number
  • Remove the first digit from a number
  • Remove the last digit from a number

1. Find the First digit of a number :

  • Note: The modulo operator gives the remainder after division
  • Iterate the given number till it becomes zero using whileloop
  • Inside whileloop,
    • Perform modulo operation and store this value as remainder
    • And at the same time, divide the given number which will return quotient which will be checked for whileloop condition
    • When while-loop breaks, the value stored in the remainder is the first digit
  • Print first digit to the console

FindFirstDigit.java

package in.bench.resources.arrays.find.number;

public class FindFirstDigit {

	public static void main(String[] args) {

		// 1. integer number
		int number = 54869;
		System.out.println("Original Number is :- \n" + number);


		// 2. find first digit using Modulo
		int remainder = 0;
		while(number != 0) {
			remainder = number % 10;
			number /= 10;
		}


		// 3. print to console
		System.out.print("\nFirst Digit is :- \n" + remainder);
	}
}

Output:

Original Number is :- 
54869

First Digit is :- 
5

2. Find the Last digit of a number :

  • The modulo operator gives the remainder after division
  • n modulo 10 provides a last digit of n
  • For example, (1234 % 10) => 4 as shown in the below illustration

FindLastDigit.java

package in.bench.resources.arrays.find.number;

public class FindLastDigit {

	public static void main(String[] args) {

		// 1. integer number
		int num = 1254;
		System.out.println("Original Number is :- \n" + num);


		// 2. find last digit using Modulo
		int remainder = num % 10;
		System.out.print("\nLast Digit is :- \n" + remainder);
	}
}

Output:

Original Number is :- 
1254

Last Digit is :- 
4

3. Remove the First digit from a number :

  • Convert the given integer/number into String using toString() method
  • Use String‘s substring() method to get String leaving first character
  • Now, convert String to Integer/number using Integer.parseInt() method

RemoveFirstDigit.java

package in.bench.resources.arrays.find.number;

public class RemoveFirstDigit {

	public static void main(String[] args) {

		// 1. integer number
		int number = 54869;
		System.out.println("Original Number is :- \n" + number);


		// 2. find first digit using Modulo
		int firstDigit = Integer.parseInt(Integer.toString(number).substring(1));


		// 3. print to console
		System.out.print("\nAfter removing first Digit from a number is :- \n" + firstDigit);
	}
}

Output:

Original Number is :- 
54869

After removing first Digit from a number is :- 
4869

4. Remove the Last digit from a number :

  • The divide operator gives the Quotient after division
  • n divide 10 provides the value removing the last digit of a number n
  • For example, (1234 / 10) => 123 as shown in the below illustration

RemoveLastDigit.java

package in.bench.resources.arrays.find.number;

public class RemoveLastDigit {

	public static void main(String[] args) {

		// 1. integer number
		int num = 1254;
		System.out.println("Original Number is :- \n" + num);


		// 2. remove last digit using divide operator
		int quotient = num / 10;
		System.out.print("\nAfter removing last Digit from a number is :- \n" 
				+ quotient);
	}
}

Output:

Original Number is :- 
1254

After removing last Digit from a number is :- 
125

Related Articles :

Happy Coding !!
Happy Learning !!

Java 8 – How to generate Fibonacci numbers using Stream ?
Java - Swapping 2 Strings without Third variable