//find number of times each digit occur in a number import java.util.Scanner; public class Numbers { public static void main(String[] args) { int num; int[] arr = new int[10]; // we use hashing to solve this problem Scanner ip = new Scanner(System.in); System.out.print("Enter a number: "); num = ip.nextInt(); while (num > 0) { int rem = num % 10; arr[rem]++; // increment the values in array. num = num / 10; } for (int i = 0; i <= 9; i++) { if (arr[i] > 0) System.out.println(i + ": " + arr[i] + " times"); } ip.close(); } } OUTPUT: Enter a number: 12345221 1: 2 times 2: 3 times 3: 1 times 4: 1 times 5: 1 times