Split Integer Into Digits using Java
Python Operators in Python (Article) Operators in Python (Program)
129Program:
import java.util.*; public class SplitDigits { public static void main(String[] args) { // Testing splitDigitsArrayList System.out.println(Arrays.toString(splitDigitsArray(123))); System.out.println(Arrays.toString(splitDigitsArray(40320))); System.out.println(Arrays.toString(splitDigitsArray(-5914133))); System.out.println(Arrays.toString(splitDigitsArray(0))); // Testing splitDigitsArray System.out.println(splitDigitsArrayList(123)); System.out.println(splitDigitsArrayList(40320)); System.out.println(splitDigitsArrayList(-5914133)); System.out.println(splitDigitsArrayList(0)); } // int --> ArrayList public static ArrayList splitDigitsArrayList(int number) { ArrayList digits = new ArrayList (); do { digits.add(0, number % 10); number /= 10; } while (number != 0); return digits; } // int --> int[] public static int[] splitDigitsArray(int number) { int[] digits = new int[(""+Math.abs(number)).length()]; for (int i = digits.length-1; i >= 0; i--) { digits[i] = number % 10; number /= 10; } return digits; } }
Output:
[1, 2, 3] [4, 0, 3, 2, 0] [-5, -9, -1, -4, -1, -3, -3] [0] [1, 2, 3] [4, 0, 3, 2, 0] [-5, -9, -1, -4, -1, -3, -3][0]
Explanation:
e332e
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.