Computer Applications
Write a program that reads a long number, counts and displays the occurrences of each digit in it.
Java
Java Arrays
31 Likes
Answer
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
Answered By
7 Likes
Related Questions
Write a program to input 10 integer elements in an array and sort them in descending order using bubble sort technique.
Write a program that reads ten integers and displays them in the reverse order in which they were read.
Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message "Search element not found".
5, 7, 9, 11, 15, 20, 30, 45, 89, 97
Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist".
Sample Input:n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 1982 1987 1993 1996 1999 2003 2006 2007 2009 2010