Skip to content

Instantly share code, notes, and snippets.

@Muku784
Created November 26, 2022 13:07
Show Gist options
  • Save Muku784/90fadf1008818c09c1dc1ae73aeebc94 to your computer and use it in GitHub Desktop.
Save Muku784/90fadf1008818c09c1dc1ae73aeebc94 to your computer and use it in GitHub Desktop.
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object
int number = scanner.nextInt(); // Read user input
int[] digits = getDigits(number); // Get the digits of the number
System.out.println(Arrays.toString(digits)); // Print the digits
} // End of main
public static int[] getDigits(int number) { // Method to get the digits of a number
int[] digits = new int[Integer.toString(number).length()]; // Create an array with the length of the number
int i = 0; // Create a counter
while (number > 0) { // While the number is greater than 0
digits[i] = number % 10; // Get the last digit of the number
number /= 10; // Remove the last digit of the number
i++; // Increase the counter
}
return digits; // Return the digits
} // End of getDigits method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment