import java.util.*; public class CountDigits{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int[] resArr; System.out.println("Enter a number"); int num=sc.nextInt(); resArr=count(num); for(int i=0;i<resArr.length;i++){ if(resArr[i]!=0){ System.out.println(i+"-"+resArr[i]); } } } private static int[] count(int num){ int[] arr=new int[10]; while(num!=0){ int digit=num%10; arr[digit]+=1; num/=10; } return arr; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter