Java Program to Extract Digits from A Given Integer


In Java programming, there are scenarios where we need to extract individual digits from an integer for further manipulation or analysis. This tutorial will guide you through the process of extracting digits from a given integer using Java.

Syntax

while (num > 0) {
int digit = num % 10;
   System.out.println(digit);
   num = num / 10;
}

The above is the syntax to extract digits from an integer in Java. We keep extracting the last digit by taking the remainder of the number with 10. We divide the number by 10 until it becomes 0.

Extract Digits Using Division and Modulus

One common approach to extract digits from an integer is by using division and modulus operations. The idea is to repeatedly divide the number by 10, extracting the remainder (representing the rightmost digit), and then updating the number by removing the extracted digit.

Example

Let's consider the integer num = 1234. By applying the above algorithm, we can extract each digit and perform desired operations on them

Explanation

  • We initialize an integer variable num with the value 1234.

  • Using a while loop, we iterate until the num is greater than 0.

  • Within each iteration, we extract the last digit of the current value of num by using the modulus operator (%) with 10. This operation gives us the remainder when num is divided by 10, representing the last digit.

  • The extracted digit is stored in the variable digit.

  • We display the value of the digit using System.out.println(digit), which prints the digit on the console.

  • After extracting the last digit, we update the value of num by dividing it by 10, using the integer division operator (/). This removes the last digit from the num and prepares it for the next iteration.

  • The loop continues until num becomes 0, indicating that all digits have been extracted and displayed.

public class Main {
   public static void main(String args[]) {
      int num = 1234;
      while (num > 0) {
         int digit = num % 10;
         System.out.println(digit);
         num = num / 10;
      }
   }
}

Output

4
3
2
1

Extracting Digits Using Built-in Methods

In this scenario, we will extract the individual digits from an integer and display them on the console.

Example 1

Let's consider an integer, num = 34512. We will extract and display the digits individually.

Explanation

  • We first convert the integer num to a string representation using Integer.toString(num).

  • Next, we iterate through each character in the string using a for loop.

  • For each character, we check if it is a digit using Character.isDigit().

  • If it is a digit, we convert it back to an integer using Character.getNumericValue() and store it in the digit variable.

  • Finally, we display each digit using System.out.println().

public class Main {
   public static void main(String[] args) {
	int num = 34512;
	String numString = Integer.toString(num);

	for (int i = 0; i < numString.length(); i++) {
	   if (Character.isDigit(numString.charAt(i))) {
	      int digit = Character.getNumericValue(numString.charAt(i));
            System.out.println("Digit: " + digit);
         }
      }
   }
}

Output

Digit: 3
Digit: 4
Digit: 5
Digit: 1
Digit: 2

Example 2

Let's consider another integer, num = 987654. We will extract and display the digits individually.

Explanation

  • We convert the integer num to a string representation using String.valueOf(num).

  • Instead of using an index-based loop, we utilize the enhanced for loop to iterate through each character in the string.

  • For each character, we check if it is a digit using Character.isDigit().

  • If it is a digit, we convert it back to an integer using Character.getNumericValue() and store it in the digit variable.

  • Finally, we display each digit using System.out.println().

public class Main {
   public static void main(String[] args) {
	int num = 987654;
	String numString = String.valueOf(num);
	for (char digitChar : numString.toCharArray()) {
	   if (Character.isDigit(digitChar)) {
            int digit = Character.getNumericValue(digitChar);
            System.out.println("Digit: " + digit);
	      }
	   }
   }
}

Output

Digit: 9
Digit: 8
Digit: 7
Digit: 6
Digit: 5
Digit: 4

Conclusion

In this tutorial, we explored the process of extracting digits from a given integer in Java. We covered two different scenarios: extracting digits using division and modulus operations and extracting and displaying digits individually.

By applying the division and modulus approach, we learned how to extract digits by repeatedly dividing the number by 10 and using the remainder as the digit. This technique allows us to extract and manipulate the digits of an integer efficiently.

We also explored the scenario of extracting and displaying digits individually. By converting the integer to a string representation and iterating through each character, we could check for digits and display them accordingly. This approach provides flexibility in further processing or analysis of the extracted digits.

Updated on: 04-Jul-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements