KnowledgeBoat Logo

Computer Applications

Write a program that reads a long number, counts and displays the occurrences of each digit in it.

Java

Java Arrays

ICSE

30 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

BlueJ output of Write a program that reads a long number, counts and displays the occurrences of each digit in it.

Answered By

7 Likes


Related Questions