W3docs

How to get the separate digits of an int number?

To get the separate digits of an integer number, you can convert the number to a string, and then use the list() function to convert the string to a list of characters.

To get the separate digits of an integer number in Java, you can convert the number to a string, and then use the toCharArray() method to convert the string to an array of characters.

Here is an example:


// Get the separate digits of the number 12345
int number = 12345;

// Convert the number to a string
String numberStr = String.valueOf(number);

// Convert the string to an array of characters
char[] digits = numberStr.toCharArray();

// Print the array of digits
System.out.println(java.util.Arrays.toString(digits));

The output will be: [1, 2, 3, 4, 5]

You can also use a loop to iterate over the characters and convert them back to integers if needed:


// Get the separate digits of the number 12345
int number = 12345;
String numberStr = String.valueOf(number);

// Iterate over the characters in the string and convert them to integers
for (char c : numberStr.toCharArray()) {
    int digit = c - '0';
    // Do something with the digit
    System.out.print(digit + " ");
}

The output will be: 1 2 3 4 5