// Kyle Thayer, CSE 142 // This program counts the occurence of digits in a number // For example: 252 has 2 2's and 1 5. public class CountDigits{ public static void main(String[] args){ int number = 878432578; // an array to hold the counts of each digit // digitCount[0] will hold the count of 0's // digitCount[1] will hold the count of 1's // etc. int[] digitCount = new int[10]; //while loop to go through each digit of number while(number > 0){ // get the next digit int digit = number % 10; // remove the last digit number = number /10; // add the count of the matching digit // eg. if digit was 1, this adds one to // digitCount[1] digitCount[digit]++; } // loop through the 10 digits in the digit counter for(int i = 0; i < digitCount.length; i++){ // print out the count of the digit i // for example, when i is 1, then the digit is 1 // and the count is saved in digitCount[1] System.out.println(i + ": " + digitCount[i]); } } }