19
Question Question 19
Write a program that reads a long number, counts and displays the occurrences of each digit in it.
import java.util.Scanner;
public class KboatSDANumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
long num = in.nextLong();
int dCount[] = new int[10];
while (num != 0) {
int d = (int)(num % 10);
dCount[d] = dCount[d] + 1;
num /= 10;
}
System.out.println("Digit\tOccurence");
for (int i = 0; i < 10; i++) {
if (dCount[i] != 0) {
System.out.println(i + "\t" + dCount[i]);
}
}
}
}Output
