Please tell me how to write a program which finds the frequency of each digit of an enterd number.
Eg- if i enter 1112245 the output should come out to be 'Frquency of 1 is 3' 'Frequency of 2 is 2' 'Frequency of 4 is 1' 'Frequency of 5 is 1'.
Plz keep the data type as integer or long.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

``````import java.io.*;
import java.lang.*;
class Frequecy
{
public static void main(String args[]) throws IOException
{
int n;
System.out.println("Enter number");
int A[]=new int[9];
int a=0;
int c=0;
for(int i=n;i!=0;i/=10)
{
int d=i%10;
A[a]=d;
a++;
}
for(int i=0;i<=9;i++)
{
for(int j=0;j<i;j++)
{
if(A[j]==i)
{c++ ;
System.out.println("The digit "+i+" occurs "+c+" times");
}
c=0;
}
}
}
}

I wrote this code but it didn't produce the desired output, when I entered '111222' the output came out as 'The digit 2 occurs 1 times' 'The digit 2 occurs 1 times' .
``````

That's a bit of a mess - looks like too much coding before too little thinking?
I suggest you put the code aside, run through a couple of cases step-by-step just using paper and pencil, then write a program that does what you just did on paper. (You will probably decide that you need just two loops - one to count the digits and one to print the counts - neither loop needs to be nested)

The code looks to be on the right track.

A few remarks... I wouldn't bother converting to an integer as it is useless, unless you actually want to do some math with it.

Look at how a letter frequency counter would be implemented... This is a big clue.

Keep the input as a string... that way you won't need that convoluted first for loop.

``````import java.io.*;
import java.lang.*;
class Frequecy

public static void main(String args[]) throws IOException
{
int n;
System.out.println("Enter number");
int A[]=new int[9];
int a=0;
int c=0;
for(int i=n;i!=0;i/=10)
{
int d=i%10;
A[a]=d;
a++;
}
for(int i=0;i<=9;i++)
{
for(int j=0;j<i;j++)
{
if(A[j]==i)
{
c++;

}
}
if(c!=0)
System.out.println("The digit "+i+" occurs "+c+" times");
c=0;
}
}
}
}
``````

Hi mishalparmar, welcome to DaniWeb.
Your post doesn't violate any actual DaniWeb rules, but here at DaniWeb we try to help people learn Java and develop their Java skills. We do NOT do people's homework for them.
Your post explains and teaches nothing. It's just a block of uncommented code that may or may not be useful. In future please help by pointing people in the right direction - eg tell them which classes and methods they should read about, or give them some sample code that they will have to understand and adapt to their needs. Don't just spoon-feed them a solution to copy and paste.

If you do post code it would be less embarrasing for you if you test that it compiles first, and when the compile errors are fixed, that it gives the right results.

I'm hesitant to post this so just remember that your instructor can find this just as fast as you can.

``````import java.io.IOException;
import java.util.Scanner;
// lang import is redundant

public class Main {
public static final String PATH = "path";

public static void main(String args[]) throws IOException
{
// try with resources syntax, learn it.
// http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

// reading information from the command line
// http://docs.oracle.com/javase/tutorial/essential/io/cl.html
try (Scanner in = new Scanner(System.in)) {
System.out.print("Enter the number: ");

int num = 0;
if (in.hasNext()) {
num = Integer.parseInt(in.nextLine());
}

int[] freqs = {0,0,0,0,0,0,0,0,0,0};

for (int i=num; i > 0; i /= 10) {
freqs[i % 10]++; // increment the appropriate digits frequency
}

int i=0;
for (int n: freqs) {
System.out.println("The number " + (i++) + " appears " + n + " times in " + num + ".");
}
}
}
}
``````