I am programming in Java and want to convert an int into an array. For example I want to convert 122 into {1, 2, 2}. Do you have an idea how this works? Thanks in advance. I am not allowed to convert it into a string.
Here is the answer without using Math class
import java.util.Arrays;
class int_to_array
{
    public static void main(String arg[])
    {
        int number = 122;
        int length=0;
        int org=number;
        while(org!=0)
        {
            org=org/10;
            length++;
        }
        int[] array = new int[length];
        for(int i = 0; i < length; i++) {
          int rem = number % 10; 
          array[length - i - 1] = rem;
          number = number / 10; 
        }
        System.out.println(Arrays.toString(array));
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With