static void printAll(int number) { while (number > 10) { int digit = number % 10; number = (number - digit) / 10; System.out.print(digit + " "); } System.out.print(number); }
Method 2:- Convert int to a String and then printing individual characters.
static void printAll(int number) { String numberString = Integer.toString(number); for(int i = numberString.length()-1 ; i >=0 ; i--){ System.out.print(numberString.charAt(i) + " "); } }
You can make it print the digits print in the reverse direction by changing the parameters of the for loop.
No comments:
Post a Comment