Frequency of every digit in a number

 Frequency of every digit in a number

Problem Description


Java program to print the frequency of every digit in a number

Logic Test Case 1


Input (stdin)

99439


Expected Output


3=1


4=1


9=3

Logic Test Case 2


Input (stdin)

23662584


Expected Output


2=2


3=1


4=1


5=1


6=2


8=1



CODE AREA



import java.io.*;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class TestClass {

   public static void main(String[] args) throws IOException {


        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //System.out.print("Enter any number : ");

        String num = br.readLine();


        int number = Integer.valueOf(num);

        // Instantiate an array to keep Digit with Frequency

        int freq[] = new int[10];

        // initialize default value to digit

        for (int i = 0; i < 10; i++) {

            freq[i] = 0;

        }

        int digit;

        // Calculate Digit with Frequency

        while (number > 0) {

            digit = number % 10;

            freq[digit]++;

            number = number / 10;

        }

        // Display Digit with Frequency

        //System.out.println("Digit \t" + "Frequency");

        for (int i = 0; i < 10; i++) {

            if (freq[i] != 0) {

                System.out.println(i + "=" + freq[i]);

            }


        }


    }


}

Post a Comment

0 Comments